Skip to content
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

DEV: Add rake task for upgrading speedscope #478

Merged
merged 3 commits into from
Jan 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,102 @@ task :client_dev do
rescue Interrupt
listener.stop
end

desc "Upgrade Speedscope to the latest version"
task :speedscope_upgrade do
require 'net/http'
require 'json'
require 'tmpdir'
require 'zip'

puts "Checking GitHub for the latest version..."
releases_uri = URI('https://api.github.com/repos/jlfwong/speedscope/releases/latest')
req = Net::HTTP::Get.new(releases_uri, { 'Accept' => 'application/vnd.github.v3+json' })
http = Net::HTTP.new(releases_uri.hostname, releases_uri.port)
http.use_ssl = true
res = http.request(req)
if res.code.to_i != 200
puts "ERROR: GitHub responded with an unexpected status code: #{res.code.to_i}."
exit
end
latest_release_info = JSON.parse(res.body)
latest_version = latest_release_info['name'].sub('v', '')

speedscope_dir = File.expand_path('./lib/html/speedscope', __dir__)
current_version = File.read(File.join(speedscope_dir, 'release.txt')).split("\n")[0].split("@")[-1]
if latest_version == current_version
puts "Speedscope is already on the latest version (#{current_version.inspect})."
exit
end
puts "Current version is #{current_version.inspect} and latest version is: #{latest_version.inspect}"
asset = latest_release_info['assets'].find { |ast| ast['content_type'] == 'application/zip' }
asset_id = asset && asset['id']
if !asset_id
puts "ERROR: Couldn't find any zip files in the #{latest_version.inspect} release. "\
"Maybe the maintainer forgot to add one or the content type has changed. "\
"Please the check the releases page of the repository and/or contact the maintainer."
exit
end
Dir.mktmpdir do |temp_dir|
puts "Downloading zip file of latest release to #{temp_dir}..."
download_uri = URI("https://api.github.com/repos/jlfwong/speedscope/releases/assets/#{asset_id}")
req = Net::HTTP::Get.new(download_uri, { 'Accept' => 'application/octet-stream' })
http = Net::HTTP.new(download_uri.hostname, download_uri.port)
http.use_ssl = true
res = http.request(req)
if res.code.to_i != 302
puts "ERROR: Expected a 302 status code from GitHub download URL but instead got #{res.code.inspect}."
exit
end
aws_uri = URI(res['Location'])
http = Net::HTTP.new(aws_uri.host, aws_uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(aws_uri)
temp_zip_file = File.join(temp_dir, "speedscope-v#{latest_version}.zip")
http.request(request) do |response|
if response.code.to_i != 200
puts "ERROR: Expected a 200 status code from download URL but instead got #{res.code.inspect}."
exit
end
open(temp_zip_file, 'w') do |io|
response.read_body do |chunk|
io.write(chunk)
end
end
end
puts "Download completed."
kept_files = File.read(File.join(speedscope_dir, '.kept-files')).split("\n").reject { |n| n.strip.start_with?('//') }
puts "Deleting existing speedscope files..."
Dir.foreach(speedscope_dir) do |name|
next if name == '.' || name == '..'
next if kept_files.include?(name)
full_path = File.join(speedscope_dir, name)
File.delete(full_path)
msg = "Deleted #{full_path}"
puts msg.rjust(msg.size + 4, ' ')
end
puts "Extracting zip files..."
Zip::File.open(temp_zip_file) do |zip_file|
zip_file.each do |entry|
next if !entry.name.start_with?('speedscope/')
next if entry.name =~ /perf-vertx-stacks/
next if entry.name =~ /README/
dest_path = File.join(File.dirname(speedscope_dir), entry.name)
entry.extract(dest_path)
msg = "Extracted #{entry.name} to #{dest_path}"
puts msg.rjust(msg.size + 4, ' ')
end
end
new_version = File.read(File.join(speedscope_dir, 'release.txt')).split("\n")[0].split("@")[-1]
if new_version != latest_version
puts "ERROR: Something went wrong. Expected the zip file to contain release #{latest_version.inspect}, "\
"but instead it contained #{new_version.inspect}. You'll need to investigate what went wrong."
exit
end
puts "Replacing Google Fonts stylesheet URL with the URL of the local copy in index.html..."
index_html_content = File.read(File.join(speedscope_dir, 'index.html'))
index_html_content.sub!('https://fonts.googleapis.com/css?family=Source+Code+Pro', 'fonts/source-code-pro-regular.css')
File.write(File.join(speedscope_dir, 'index.html'), index_html_content)
puts "All done!"
end
end
6 changes: 6 additions & 0 deletions lib/html/speedscope/.kept-files
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// the speedscope_upgrade rake task upgrades the speedscope files
// in this directory. This file marks which files in this directory
// that should not be deleted by an upgrade.
fonts
README.md
.kept-files
4 changes: 2 additions & 2 deletions lib/html/speedscope/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
This directory contains the necessary files and assets to run [speedscope](https://github.com/jlfwong/speedscope). If you wish to update speedscope to a new version, head over to the [releases](https://github.com/jlfwong/speedscope/releases) page on Github, download the `.zip` file of the release you want to upgrade to and unzip it right here inside this directory. Be sure to delete the `perf-vertx-stacks-01-collapsed-all.*.txt` and `README` files that are in the `.zip` file after you unzip it.
This directory contains the necessary files and assets to run [speedscope](https://github.com/jlfwong/speedscope). The files come from a ZIP file that can be downloaded from the releases page of the speedscope repository on GitHub. If you wish to upgrade speedscope to the latest version, run `bundle exec rake speedscope_upgrade` in the root directory of this repository.

We're currently on version 1.12.1 of speedscope.
The rake task will download the ZIP file of the latest speedscope release from Github, remove the old files from the `lib/html/speedscope` directory and extract the ZIP file into the same directory. It will also make a small change to the `index.html` file to replace a link to a Google Fonts stylesheet with a link to a local copy in the `fonts` directory. This is done to make sure speedscope doesn't load any assets from third-parties so that it can work when you're on an intranet.
8 changes: 8 additions & 0 deletions lib/html/speedscope/fonts/source-code-pro-regular.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
@font-face {
font-family: 'Source Code Pro';
font-style: normal;
font-weight: 400;
src: local(''),
url('./source-code-pro-v13-regular.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
url('./source-code-pro-v13-regular.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/html/speedscope/index.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>speedscope</title><link href="https://fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet"><script></script><link rel="stylesheet" href="reset.8c46b7a1.css"><link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.bc503437.png"><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.f74b3187.png"></head><body> <script src="speedscope.44364064.js"></script>
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title>speedscope</title><link href="fonts/source-code-pro-regular.css" rel="stylesheet"><script></script><link rel="stylesheet" href="reset.8c46b7a1.css"><link rel="icon" type="image/png" sizes="32x32" href="favicon-32x32.bc503437.png"><link rel="icon" type="image/png" sizes="16x16" href="favicon-16x16.f74b3187.png"></head><body> <script src="speedscope.44364064.js"></script>
</body></html>
1 change: 1 addition & 0 deletions rack-mini-profiler.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'webpacker'
s.add_development_dependency 'rails', '~> 6.0'
s.add_development_dependency 'webmock', '3.9.1'
s.add_development_dependency 'rubyzip'

s.require_paths = ["lib"]
end