Skip to content

Instantly share code, notes, and snippets.

@skunkworker
Last active March 7, 2017 23:13
Show Gist options
  • Save skunkworker/a2d3dd53e0352434ca81da264b4108f1 to your computer and use it in GitHub Desktop.
Save skunkworker/a2d3dd53e0352434ca81da264b4108f1 to your computer and use it in GitHub Desktop.
Package chrome extension
# Chrome Extension Packer
#
# This removes .git, development files and other files specified and then packages up a chrome extension.
# By default the manifest.json that lives in the directory is removed (local development links etc). And replaced with the manifest.json.production file.
require 'find'
require 'fileutils'
require 'json'
extension_directory = "/chrome/"
@production_extension = "production"
@development_extension = %w(development)
@purge_file_types = %w(idraw svg)
@remove_files = ".DS_Store"
@development_files = %w( manifest.json )
@production_files = %w( manifest.json.production )
@output_filename = "smt-chrome"
@output_directory = Dir.pwd
@remove_git = true
def read_in_manifest_version(manifest_file)
@version ||= JSON.parse(File.read(manifest_file))["version"]
end
def walk(start)
Dir.foreach(start) do |x|
path = File.join(start, x)
if x == "." or x == ".."
next
elsif File.directory?(path)
# puts path + "/" # remove this line if you want; just prints directories
walk(path)
else
if File.basename(path) == "manifest.json"
read_in_manifest_version(path)
end
# puts x
end
end
end
def walk_remove(start, filename)
Dir.foreach(start) do |x|
path = File.join(start, x)
if x == "." or x == ".."
next
elsif File.directory?(path)
# puts path + "/" # remove this line if you want; just prints directories
walk_remove(path, filename)
else
if File.basename(path) == filename
puts "Removing File [#{filename}]"
FileUtils.remove_file(path)
return
end
# puts x
end
end
end
def walk_rename(start, filename)
Dir.foreach(start) do |x|
path = File.join(start, x)
if x == "." or x == ".."
next
elsif File.directory?(path)
# puts path + "/" # remove this line if you want; just prints directories
walk_rename(path, filename)
else
if File.basename(path) == filename
new_file_name = path.gsub(".#{@production_extension}","")
puts "Renaming File [#{filename}] to [#{new_file_name}]"
FileUtils.mv(path, new_file_name)
# FileUtils.remove_file(path)
return
end
# puts x
end
end
end
def zip_directory(source)
filename = @output_filename
# path = Rails.root.to_s + "/public/tmpFiles"
archive = @output_directory + "/" + filename + "@" + @version + ".zip"
# puts "Path: #{source}"
# puts "Archive: #{archive}"
`rm "#{archive}"` if File.exists?(archive) # remove the archive file, if it exists.
`zip -r "#{archive}" "#{source}"` # zip the contents of the directory
puts "Created Release Zip: #{archive}"
end
# copy files into new directory
# traverse directory.
# remove purge_file_types
# skips the .git folder
def copy_without_git(source_path, target_path)
Find.find(source_path) do |source|
target = source.sub(/^#{source_path}/, target_path)
if File.directory? source
Find.prune if File.basename(source) == '.git'
FileUtils.mkdir target unless File.exists? target
else
if @purge_file_types.include?(File.extname(source).gsub(".","")) || @remove_files.include?(File.basename(source))
puts "Removing file #{File.basename(source)}"
else
FileUtils.copy source, target
end
end
end
end
def remove_development_files(source)
@development_files.each do |filename|
walk_remove(source, filename)
end
end
def rename_production_files(source)
@production_files.each do |filename|
walk_rename(source, filename)
end
end
# remove development files
# rename production files
# zip
# delete directory
temp_directory = Dir.pwd + "/chrome_temp/"
full_directory = Dir.pwd + "/" + extension_directory
puts "Copying files"
copy_without_git full_directory, temp_directory
walk(temp_directory)
puts "Removing and renaming files"
remove_development_files(temp_directory)
rename_production_files(temp_directory)
puts "Zipping temp directory"
zip_directory(temp_directory)
puts "Removing temp directory"
FileUtils.rm_r temp_directory
@skunkworker
Copy link
Author

Added reading in manifest.json to put the version in the filename.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment