-
Notifications
You must be signed in to change notification settings - Fork 14
Vendor speedscope with gem #56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5879405
Vendor speedscope with gem
Envek 6ba1be4
Add basic specs
Envek 0dfd1ca
Add note about speedscope version bump
Envek 2821076
Update lib/singed/speedscope.rb
Envek db7b9b1
Merge remote-tracking branch 'upstream/main' into package-speedscope
Envek b49fc61
Move all dev dependencies to Gemfile
Envek ab8444a
Merge branch 'main' into package-speedscope
Envek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,3 +7,4 @@ | |
| /spec/reports/ | ||
| /tmp/ | ||
| /vendor/bundle | ||
| /vendor/speedscope | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,5 @@ source "https://rubygems.org" | |
| gemspec | ||
|
|
||
| gem "rake", "~> 13.0" | ||
| gem "rubyzip" | ||
| gem "standard" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,55 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "bundler/gem_tasks" | ||
| require "open-uri" | ||
| require "fileutils" | ||
| require "tmpdir" | ||
| require "zip" | ||
| require_relative "lib/singed/speedscope" | ||
|
|
||
| namespace :speedscope do | ||
| destination_dir = File.expand_path("vendor/speedscope", __dir__) | ||
|
|
||
| desc "Download and unpack speedscope into vendor/speedscope" | ||
| task vendor: destination_dir | ||
|
|
||
| directory destination_dir do | ||
| version = Singed::Speedscope::VERSION | ||
| url = "https://github.com/jlfwong/speedscope/releases/download/v#{version}/speedscope-#{version}.zip" | ||
|
|
||
| unzip_dir = File.expand_path("..", destination_dir) # speedscope dir is in the archive | ||
| FileUtils.mkdir_p(destination_dir) | ||
|
|
||
| tmp_zip = File.join(Dir.tmpdir, "speedscope-#{version}.zip") | ||
|
|
||
| puts "Downloading speedscope from #{url}" | ||
| URI.parse(url).open do |remote| | ||
| File.open(tmp_zip, "wb") do |file| | ||
| IO.copy_stream(remote, file) | ||
| end | ||
| end | ||
|
|
||
| puts "Vendoring speedscope into #{unzip_dir}" | ||
| Zip::File.open(tmp_zip) do |zip_file| | ||
| zip_file.each do |entry| | ||
| destination = File.join(unzip_dir, entry.name) | ||
| if entry.directory? | ||
| FileUtils.mkdir_p(destination) | ||
| else | ||
| FileUtils.mkdir_p(File.dirname(destination)) | ||
| entry.extract(destination_directory: unzip_dir) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| desc "Remove the unpacked speedscope directory" | ||
| task :clobber do | ||
| FileUtils.rm_rf(destination_dir) | ||
| end | ||
| end | ||
|
|
||
| Rake::Task[:build].enhance ["speedscope:vendor"] | ||
| Rake::Task[:clobber].enhance ["speedscope:clobber"] | ||
|
|
||
| task default: %i[] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rbconfig" | ||
| require "tmpdir" | ||
|
|
||
| module Singed | ||
| module Speedscope | ||
| VERSION = "1.24.0" | ||
|
|
||
| class << self | ||
| def bundled_index_html | ||
| File.join(File.expand_path("../..", __dir__), "vendor", "speedscope", "index.html") | ||
| end | ||
|
|
||
| def open_command(profile_path) | ||
| if File.exist?(bundled_index_html) | ||
| "#{os_open_command} file://#{bundled_index_html}#localProfilePath=#{profile_path}" | ||
| else | ||
| "npx speedscope #{profile_path}" | ||
| end | ||
| end | ||
|
|
||
| def open(profile_path) | ||
| profile_path = profile_path.to_s | ||
|
|
||
| if File.exist?(bundled_index_html) | ||
| open_with_bundled_speedscope(profile_path) | ||
| else | ||
| open_with_npx(profile_path) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def open_with_npx(profile_path) | ||
| system("npx", "speedscope", profile_path) | ||
| end | ||
|
|
||
| # Based on speedscope CLI code (MIT license) | ||
| # See https://github.com/jlfwong/speedscope/blob/3613918de0dd55a263d0d04f85b0c8c2039c7bee/bin/cli.mjs | ||
| def open_with_bundled_speedscope(profile_path) | ||
| source_buffer = File.binread(profile_path) | ||
| filename = File.basename(profile_path) | ||
|
|
||
| source_base64 = [source_buffer].pack("m0") | ||
| js_source = "speedscope.loadFileFromBase64(#{filename.inspect}, #{source_base64.inspect})" | ||
|
|
||
| file_prefix = "speedscope-#{Time.now.to_i}-#{Process.pid}" | ||
| js_path = File.join(Dir.tmpdir, "#{file_prefix}.js") | ||
| File.write(js_path, js_source) | ||
|
|
||
| url_to_open = "file://#{File.expand_path(bundled_index_html)}#localProfilePath=#{js_path}" | ||
|
|
||
| # See https://github.com/jlfwong/speedscope/blob/3613918de0dd55a263d0d04f85b0c8c2039c7bee/bin/cli.mjs#L96-L105 | ||
| host_os = RbConfig::CONFIG["host_os"] | ||
| if host_os =~ /mswin|mingw|cygwin/ || host_os =~ /darwin/ | ||
| html_path = File.join(Dir.tmpdir, "#{file_prefix}.html") | ||
| File.write(html_path, "<script>window.location=#{url_to_open.inspect}</script>") | ||
| url_to_open = "file://#{html_path}" | ||
| end | ||
|
|
||
| system os_open_command, url_to_open | ||
| end | ||
|
|
||
| def os_open_command | ||
| case host_os = RbConfig::CONFIG["host_os"] | ||
| when /mswin|mingw|cygwin/ | ||
| "start" | ||
| when /darwin/ | ||
| "open" | ||
| when /linux|bsd/ | ||
| "xdg-open" | ||
| else | ||
| raise "Unsupported OS to open browser: #{host_os}" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.