Module: Gitplate

Defined in:
lib/gitplate/plate.rb,
lib/gitplate/gitplate.rb

Defined Under Namespace

Modules: Outputs Classes: Plate

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.clear_directory(dir) ⇒ Object



162
163
164
165
166
167
# File 'lib/gitplate/gitplate.rb', line 162

def self.clear_directory(dir)
  if (File.directory?(dir))
    full_path = File.expand_path(dir)
    FileUtils.rm_rf full_path
  end
end

.config_fileObject



8
9
10
# File 'lib/gitplate/gitplate.rb', line 8

def self.config_file
  "#{gitplate_dir}/config.yml"
end

.debug_msg(msg) ⇒ Object



149
150
151
# File 'lib/gitplate/gitplate.rb', line 149

def self.debug_msg(msg)
  puts msg.color(:cyan).bright
end

.ensure_gitplate_dirObject



126
127
128
# File 'lib/gitplate/gitplate.rb', line 126

def self.ensure_gitplate_dir
  init_gitplate_dir false
end

.fatal_msg(msg) ⇒ Object



153
154
155
# File 'lib/gitplate/gitplate.rb', line 153

def self.fatal_msg(msg)
  puts msg.color(:red).bright
end

.fatal_msg_and_fail(msg) ⇒ Object



157
158
159
160
# File 'lib/gitplate/gitplate.rb', line 157

def self.fatal_msg_and_fail(msg)
  fatal_msg msg
  raise msg
end

.format_hash_for_yaml(hash) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/gitplate/gitplate.rb', line 169

def self.format_hash_for_yaml(hash)
  result = Hash.new

  hash.each do |name, value|
    if (value.kind_of? Hash)
      result[name.to_s] = format_hash_for_yaml value
    else
      result[name.to_s] = value
    end
  end

  result
end

.get_plate_repository(project_name, repository) ⇒ Object



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
# File 'lib/gitplate/gitplate.rb', line 35

def self.get_plate_repository(project_name, repository)
  source_repository_sha = ""

  Dir.chdir project_name do
    # create a gitplate directory structure for checking out the repository to
    out_path = File.expand_path(File.join(gitplate_dir, 'tmp', 'checkout'))
    FileUtils.mkdir_p out_path

    archive_file = File.expand_path(File.join(gitplate_dir, 'tmp', "#{project_name}.zip"))

    Dir.chdir out_path do
      source_repository = Git.clone(repository, project_name)
      source_repository_sha = source_repository.object('HEAD').sha

      source_repository.archive(source_repository_sha, archive_file, :format => "zip")
    end

    unzip_file archive_file, Dir.pwd

    # get rid of the temporary directory
    clear_directory File.expand_path(File.join(gitplate_dir, 'tmp'))
  end

  source_repository_sha
end

.gitplate_dirObject



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

def self.gitplate_dir
  '.gitplate'
end

.info_msg(msg) ⇒ Object



145
146
147
# File 'lib/gitplate/gitplate.rb', line 145

def self.info_msg(msg)
  puts msg.color(:green).bright
end

.initObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gitplate/gitplate.rb', line 20

def self.init
  init_gitplate_dir

  if (!File.exists?(plate_file))
    debug_msg "Creating sample plate file #{plate_file}"

    # create the sample plate file
    File.open(plate_file, "w") { |f|
      f.write("init do\n")
      f.write("  # add code to run when installing a new project\n")
      f.write("end\n")
    }
  end
end

.init_gitplate_dir(update_version = true) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/gitplate/gitplate.rb', line 111

def self.init_gitplate_dir(update_version = true)
  if (!File.directory? gitplate_dir)
    FileUtils.mkdir_p gitplate_dir
  end

  if (!File.exists?(config_file))
    debug_msg "Creating config file"
    File.open(config_file, 'w') { |f| YAML.dump({}, f) }
  end

  if (update_version)
    update_config_with({ :gitplate => { :init_version => Gitplate::VERSION } })
  end
end

.install(project_name, repository) ⇒ Object



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
93
94
95
96
97
# File 'lib/gitplate/gitplate.rb', line 61

def self.install(project_name, repository)
  if (File.directory? project_name)
    fatal_msg_and_fail "Directory already exists"
  end

  info_msg "creating #{project_name} based on #{repository}"

  FileUtils.mkdir_p project_name
  
  source_repository_sha = get_plate_repository(project_name, repository)

  # we've got the repository cloned and cleaned up of existing git history
  Dir.chdir project_name do
    ensure_gitplate_dir
    
    update_config_with({
        :project => { :name => project_name },
        :repository => { :url => repository, :sha => source_repository_sha }
      })

    # pull in the plate file from the cloned repository
    if (File.exists?(plate_file))
      Gitplate::Plate.instance.run(
          plate_file,
          {
            :project_name => project_name,
            :project_dir => Dir.pwd
          })
    else
      debug_msg "no plate file found in repository"
    end

    g = Git.init
    g.add
    g.commit "Initial commit"
  end
end

.load_gitplate_fileObject



130
131
132
# File 'lib/gitplate/gitplate.rb', line 130

def self.load_gitplate_file
  YAML.load(File.open(config_file))
end

.plate_fileObject



12
13
14
# File 'lib/gitplate/gitplate.rb', line 12

def self.plate_file
  "#{gitplate_dir}/plate"
end

.task(task, args) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/gitplate/gitplate.rb', line 99

def self.task(task, args)
  config = load_gitplate_file

  Gitplate::Plate.instance.run_task(
      plate_file,
      task,
      {
        :project_name => config["project"]["name"],
        :project_dir => Dir.pwd
      })
end

.unzip_file(file, destination) ⇒ Object



183
184
185
186
187
188
189
190
191
# File 'lib/gitplate/gitplate.rb', line 183

def self.unzip_file(file, destination)
    Zip::ZipFile.open(file) { |zip_file|
      zip_file.each { |f|
        f_path=File.join(destination, f.name)
        FileUtils.mkdir_p(File.dirname(f_path))
        zip_file.extract(f, f_path) unless File.exist?(f_path)
     }
  }
end

.update_config_with(values) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/gitplate/gitplate.rb', line 134

def self.update_config_with(values)
  config = load_gitplate_file

  formatted = format_hash_for_yaml values
  formatted.each do |name, value|
    config[name.to_s] = value
  end

  File.open(config_file, 'w') { |f| YAML.dump(config, f) }
end