Class: PgMigrate::Package

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_migrate/package.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(manifest_reader) ⇒ Package

Returns a new instance of Package.



18
19
20
21
22
# File 'lib/pg_migrate/package.rb', line 18

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_readerObject

Returns the value of attribute manifest_reader.



16
17
18
# File 'lib/pg_migrate/package.rb', line 16

def manifest_reader
  @manifest_reader
end

Instance Method Details

#build_gem(gemspec_path, output_dir) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/pg_migrate/package.rb', line 98

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
    if defined?(Gem::Builder)
      Gem::Builder.new(spec).build
    else
      Gem::Package.build(spec)
    end
  end
  #Gem::Package.build spec, false
end

#copy_schema(built_migration_path, output_dir) ⇒ Object



94
95
96
# File 'lib/pg_migrate/package.rb', line 94

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



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
87
88
89
90
91
92
# File 'lib/pg_migrate/package.rb', line 29

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

  author = "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



24
25
26
27
# File 'lib/pg_migrate/package.rb', line 24

def package(built_migration_path, output_dir, name, version, options={:force=>true})
  gemspec = create_gem(built_migration_path, output_dir, name, version, options[: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



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pg_migrate/package.rb', line 138

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



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/pg_migrate/package.rb', line 119

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