require 'docman/version' require 'yaml' require 'configliere' require 'pathname' require 'fileutils' require 'docman/git' module Docman class Docman def initialize() @app_root_dir = Pathname(__FILE__).dirname.parent @current_dir = Dir.pwd # TODO: Define workspace properly @workspace_dir = @current_dir @docroot_dir = @workspace_dir @docroot_config_dir = File.join(@docroot_dir, 'config') @docroot_structure = directory_hash File.join(@docroot_config_dir, 'master') Settings.read File.join(@app_root_dir, 'config', 'config.yaml') Settings.resolve! @app_settings = Settings @environments = Settings['environments'] @deploy_targets = Settings['deploy_targets'] end def directory_hash(path, prefix = '') return unless File.file? File.join(path, 'info.yaml') name = File.basename path prefix = prefix.size > 0 ? File.join(prefix, name) : name info = YAML::load_file(File.join(path, 'info.yaml')) info['full_path'] = path info['build_path'] = prefix info['name'] = name data = {:data => info} data[:children] = children = [] Dir.foreach(path) do |entry| next if (entry == '..' || entry == '.' || entry == '_states') full_path = File.join(path, entry) if File.directory?(full_path) dir_hash = directory_hash(full_path, prefix) unless dir_hash == nil children << dir_hash end end end data end def build(target, state) @target = target @state = state traverse_docroot_structure end def deploy end def traverse_docroot_structure(dir = nil) dir = dir ? dir : @docroot_structure build_dir dir[:data] dir[:children].each do |child| traverse_docroot_structure child end end def build_dir(info) case get_build_type info when 'dir' build_dir_dir info when 'repo' build_dir_repo info when 'subrepo' build_dir_subrepo info end end def get_version(info) info['states'][@state] end def get_deploy_target @deploy_targets[@environments[@target]['deploy_target']] end def get_build_type(info) get_deploy_target['build_types'][info['type']] end def build_dir_dir(info) FileUtils::mkdir_p get_path_in_docroot info['build_path'] end def build_dir_repo(info) repo = info['repo'] build_path = get_path_in_docroot info['build_path'] version_type = get_version(info)['type'] version = get_version(info)['version'] Git.new.get_repo(repo, build_path, version_type, version) end def build_dir_subrepo(info) end def get_path_in_docroot(path) File.join(@docroot_dir, path) end end end