lib/gjp/project.rb in gjp-0.18.0 vs lib/gjp/project.rb in gjp-0.19.0

- old
+ new

@@ -5,27 +5,25 @@ module Gjp # encapsulates a Gjp project directory class Project include Logger - # list of possible statuses - @@statuses = [:dry_running] - attr_accessor :full_path def initialize(path) @full_path = Gjp::Project.find_project_dir(File.expand_path(path)) + @git = Gjp::Git.new end # finds the project directory up in the tree, like git does def self.find_project_dir(starting_dir) result = starting_dir while is_project(result) == false and result != "/" result = File.expand_path("..", result) end - raise ArgumentError, "This is not a gjp project directory" if result == "/" + raise NotGjpDirectoryException if result == "/" result end # returns true if the specified directory is a valid gjp project @@ -36,13 +34,11 @@ end # inits a new project directory structure def self.init(dir) Dir.chdir(dir) do - if Dir.exists?(".git") == false - `git init` - end + Gjp::Git.init Dir.mkdir "src" Dir.mkdir "kit" # populate the project with templates and take a snapshot @@ -62,37 +58,38 @@ # starts a dry running phase: files added to kit/ will be added # to the kit package, src/ will be reset at the current state # when finished def dry_run from_directory do - status = get_status - if status == :dry_running + if is_dry_running return false end - set_status :dry_running take_snapshot "Dry-run started", :dry_run_started end true end + # returns true iff we are currently dry-running + def is_dry_running + latest_tag_count(:dry_run_started) > latest_tag_count(:dry_run_finished) + end + # ends any phase that was previously started, # generating file lists def finish from_directory do - status = get_status - if status == :dry_running + if is_dry_running take_snapshot "Changes during dry-run" update_output_file_lists take_snapshot "File list updates" - revert "src", :dry_run_started + @git.revert_whole_directory("src", latest_tag(:dry_run_started)) take_snapshot "Sources reverted as before dry-run" - set_status nil take_snapshot "Dry run finished", :dry_run_finished return true end end @@ -112,16 +109,16 @@ else [] end new_tracked_files = ( - `git diff-tree --no-commit-id --name-only -r #{latest_tag(:dry_run_started)} HEAD`.split("\n") - .select { |file| file.start_with?(directory) } - .map { |file|file[directory.length + 1, file.length] } - .concat(tracked_files) - .uniq - .sort + @git.changed_files_since(latest_tag(:dry_run_started)) + .select { |file| file.start_with?(directory) } + .map { |file|file[directory.length + 1, file.length] } + .concat(tracked_files) + .uniq + .sort ) log.debug("writing file list for #{directory}: #{new_tracked_files.to_s}") File.open(list_file, "w+") do |file_list| @@ -131,86 +128,33 @@ end end end end - # adds the project's whole contents to git - # if tag is given, commit is tagged - def take_snapshot(message, tag = nil) - log.debug "committing with message: #{message}" - - `git rm -r --cached --ignore-unmatch .` - `git add .` - `git commit -m "#{message}"` - - if tag != nil - `git tag gjp_#{tag}_#{latest_tag_count(tag) + 1}` + # takes a revertable snapshot of this project + def take_snapshot(message, prefix = nil) + tag = if prefix + "#{prefix}_#{latest_tag_count(prefix) + 1}" + else + nil end + + @git.commit_whole_directory(message, tag) end # returns the last tag of its type corresponding to a # gjp snapshot def latest_tag(tag_type) - "gjp_#{tag_type}_#{latest_tag_count(tag_type)}" + "#{tag_type}_#{latest_tag_count(tag_type)}" end # returns the last tag count of its type corresponding # to a gjp snapshot def latest_tag_count(tag_type) - `git tag`.split.map do |tag| - if tag =~ /^gjp_#{tag_type}_([0-9]+)$/ - $1.to_i - else - 0 - end - end.max or 0 + @git.get_tag_maximum_suffix(tag_type) end - # reverts path contents as per latest tag - def revert(path, tag) - `git rm -rf --ignore-unmatch #{path}` - `git checkout -f #{latest_tag(tag)} -- #{path}` - - `git clean -f -d #{path}` - end - - # returns a symbol with the current status - # flag - def get_status - from_directory do - @@statuses.each do |status| - if File.exists?(status_file_name(status)) - return status - end - end - end - - nil - end - - # sets a project status flag. if status = nil, - # clears all status flags - def set_status(status) - from_directory do - @@statuses.each do |a_status| - file_name = status_file_name(a_status) - if File.exists?(file_name) - File.delete(file_name) - end - - if a_status == status - FileUtils.touch(file_name) - end - end - end - end - - # returns a file name that represents a status - def status_file_name(status) - ".#{status.to_s}" - end - # runs a block from the project directory or a subdirectory def from_directory(subdirectory = "") Dir.chdir(File.join(@full_path, subdirectory)) do yield end @@ -221,13 +165,16 @@ def name File.basename(@full_path) end def version - `git rev-parse --short #{latest_tag(:dry_run_finished)}`.strip + latest_tag_count(:dry_run_finished) end def get_binding binding end + end + + class NotGjpDirectoryException < Exception end end