Class: PgMigrate::Package
- Inherits:
-
Object
- Object
- PgMigrate::Package
- Defined in:
- lib/pg_migrate/package.rb
Instance Attribute Summary collapse
-
#manifest_reader ⇒ Object
Returns the value of attribute manifest_reader.
Instance Method Summary collapse
- #build_gem(gemspec_path, output_dir) ⇒ Object
- #copy_schema(built_migration_path, output_dir) ⇒ Object
- #create_gem(built_migration_path, output_dir, name, version, force) ⇒ Object
-
#initialize(manifest_reader) ⇒ Package
constructor
A new instance of Package.
- #package(built_migration_path, output_dir, name, version, options = {:force=>true}) ⇒ Object
-
#run_template(template, opt, output_filepath) ⇒ Object
given an input template and binding, writes to an output file.
- #userfiles(built_migration_path, name) ⇒ Object
Constructor Details
#initialize(manifest_reader) ⇒ Package
Returns a new instance of Package.
12 13 14 15 16 |
# File 'lib/pg_migrate/package.rb', line 12 def initialize(manifest_reader) @log = Logging.logger[self] @manifest_reader = manifest_reader @template_dir = File.join(File.dirname(__FILE__), 'package_templates') end |
Instance Attribute Details
#manifest_reader ⇒ Object
Returns the value of attribute manifest_reader.
10 11 12 |
# File 'lib/pg_migrate/package.rb', line 10 def manifest_reader @manifest_reader end |
Instance Method Details
#build_gem(gemspec_path, output_dir) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/pg_migrate/package.rb', line 92 def build_gem(gemspec_path, output_dir) @log.debug "building gem" @log.debug "loading gem specification #{gemspec_path}" spec = Gem::Specification.load(gemspec_path) if spec.nil? raise 'unable to build gem from specification' end @log.debug "packaging gem" Dir.chdir(output_dir) do Gem::Builder.new(spec).build end #Gem::Package.build spec, false end |
#copy_schema(built_migration_path, output_dir) ⇒ Object
88 89 90 |
# File 'lib/pg_migrate/package.rb', line 88 def copy_schema(built_migration_path, output_dir) FileUtils.cp_r(File.join(built_migration_path, '.'), output_dir) end |
#create_gem(built_migration_path, output_dir, name, version, force) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/pg_migrate/package.rb', line 23 def create_gem (built_migration_path, output_dir, name, version, force) # validate that manifest is valid @log.debug "validating output dir is manifest" if !FileTest::exist?(built_migration_path) raise "built manifest path does not exist #{built_migration_path}" end if built_migration_path == output_dir raise "source and destination can not be the same path" end loaded_manifest = @manifest_reader.load_input_manifest(built_migration_path) @manifest_reader.validate_migration_paths(built_migration_path, loaded_manifest) @log.debug "preparing to build gem" target = File.join(output_dir, name) # stolen almost verbatim from bundler: https://github.com/carlhuda/bundler/blob/master/lib/bundler/cli.rb constant_name = name.split('_').map { |p| p[0..0].upcase + p[1..-1] }.join constant_name = constant_name.split('-').map { |q| q[0..0].upcase + q[1..-1] }.join('::') if constant_name =~ /-/ constant_array = constant_name.split('::') # end stolen = "pgmigrate" email = "[email protected]" pg_migrate_version = PgMigrate::VERSION gemfiles = ["Gemfile", "#{name}.gemspec", "lib/#{name}.rb", "lib/#{name}/version.rb", "bin/#{name}"] gemfiles += userfiles(built_migration_path, name) gemspec_path = File.join(output_dir, "#{name}.gemspec") @log.debug "building gem" output = Pathname.new(output_dir) if !output.exist? if !force raise "Output directory '#{output_dir}' does not exist. Create it or specify force=true" else output.mkpath end else # verify that it's is a directory if !output.directory? raise "output_dir #{output_dir} is a file; not a directory." else @log.debug("deleting & recreating existing output_dir #{output_dir}") output.rmtree output.mkpath end end FileUtils.mkdir_p(output_dir) FileUtils.mkdir_p(File.join(output_dir, "bin")) FileUtils.mkdir_p(File.join(output_dir, "lib", name)) run_template("Gemfile.erb", binding, File.join(output_dir, "Gemfile")) run_template("gemspec.erb", binding, gemspec_path) run_template("lib/gem.rb", binding, File.join(output_dir, "lib", "#{name}.rb")) run_template("lib/gem/version.rb", binding, File.join(output_dir, "lib", name, "version.rb")) run_template("bin/migrate.rb", binding, File.join(output_dir, "bin", "#{name}")) copy_schema(built_migration_path, File.join(output_dir, "lib", name, "schemas")) return gemspec_path end |
#package(built_migration_path, output_dir, name, version, options = {:force=>true}) ⇒ Object
18 19 20 21 |
# File 'lib/pg_migrate/package.rb', line 18 def package(built_migration_path, output_dir, name, version, ={:force=>true}) gemspec = create_gem(built_migration_path, output_dir, name, version, [:force]) build_gem(gemspec, output_dir) end |
#run_template(template, opt, output_filepath) ⇒ Object
given an input template and binding, writes to an output file
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/pg_migrate/package.rb', line 128 def run_template(template, opt, output_filepath) bootstrap_template = nil File.open(File.join(@template_dir, template), 'r') do |reader| bootstrap_template = reader.read end template = ERB.new(bootstrap_template, 0, "%<>") content = template.result(opt) File.open(output_filepath, 'w') do |writer| writer.syswrite(content) end end |
#userfiles(built_migration_path, name) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/pg_migrate/package.rb', line 109 def userfiles(built_migration_path, name) gempaths = [] Find.find(built_migration_path) do |path| if path == ".." Find.prune else # make relative relative = path[built_migration_path.length..-1] gempath = File.join("lib", name, "schemas", relative) gempaths.push(gempath) end end return gempaths end |