require "rake" require "pathname" module DtcRake def configure(&block) config = Config.instance yield config config.validate! end module_function :configure class Config include Singleton def initialize self.root_dir = Dir.pwd self.output_dir = "target" self.colorize = true self.upload_readme = false self.appbox_meta_artifact_code = "UU.OS/RUNTIME/APP_BOX" self.upload_uucloud_descriptor = false self.version_files = [] guess_vendor_and_app end # Code of application. Guessed from root_dir name if not set. attr_accessor :app # Code of appbox artifact. Guessed from product_code and appbox_version # if not set. attr_accessor :appbox_artifact_code # Code of appbox location (folder or organization unit). Required. attr_accessor :appbox_location_code # Code of appbox meta artifact. Default value: UU.OS/RUNTIME/APP_BOX. attr_accessor :appbox_meta_artifact_code # Code of territory where the appbox artifact is / should be. Required. attr_accessor :appbox_territory_code # Path to uarchive with contents of appbox artifact. Default value: nil. # If not set, appbox is created with empty content. # Relative path is relative to root_dir. attr_accessor :appbox_uarchive # Appbox version. Read from VERSION file located in the same directory # as Rakefile if not set. attr_accessor :appbox_version # Print messages in colors. Default value: true. attr_accessor :colorize # Name of folder with build products. Default value: target. # Relative path is relative to root_dir. attr_accessor :output_dir # Code of product the appbox belongs to. Required. attr_accessor :product_code # Name of product the appbox belongs to. Required. attr_accessor :product_name # Appbox project root folder. Default value: current working directory. attr_accessor :root_dir # Upload README.md or README.txt to appbox artifact. Default value: false. attr_accessor :upload_readme # Upload uuApp deployment descriptor to appbox artifact. Default value: false. attr_accessor :upload_uucloud_descriptor # Path to uuApp deployment descriptor. Default value: uucloud_descriptor.json. attr_accessor :uucloud_descriptor_path # Code of vendor. Guessed from root_dir name if not set. attr_accessor :vendor # List of files (or glob patterns) determining which files get updated by # version:* tasks. Default: none. attr_accessor :version_files def validate! %w(appbox_location_code appbox_territory_code product_code product_name).each do |param| if instance_variable_get("@#{param}").nil? _error "Required parameter #{param} is not set. Set it within DtcRake.configure in your Rakefile." end end end def app=(name) @app = name.downcase unless name.nil? end def appbox_artifact_code return @appbox_artifact_code if @appbox_artifact_code @appbox_artifact_code = ENV["DTC_RAKE_APPBOX_ARTIFACT_CODE"] if @appbox_artifact_code.nil? default_code = "#{product_code}/APPBOX-#{appbox_version}" @appbox_artifact_code = default_code end @appbox_artifact_code end def appbox_artifact_code=(code) env = ENV["DTC_RAKE_APPBOX_ARTIFACT_CODE"] # environment variable takes precedence @appbox_artifact_code = env.nil? ? code : env @appbox_artifact_code end def appbox_location_code return @appbox_location_code if @appbox_location_code @appbox_location_code = ENV["DTC_RAKE_APPBOX_LOCATION_CODE"] @appbox_location_code end def appbox_location_code=(code) env = ENV["DTC_RAKE_APPBOX_LOCATION_CODE"] # environment variable takes precedence @appbox_location_code = env.nil? ? code : env @appbox_location_code end def appbox_meta_artifact_code return @appbox_meta_artifact_code if @appbox_meta_artifact_code @appbox_meta_artifact_code = ENV["DTC_RAKE_APPBOX_META_ARTIFACT_CODE"] @appbox_meta_artifact_code end def appbox_meta_artifact_code=(code) env = ENV["DTC_RAKE_APPBOX_META_ARTIFACT_CODE"] # environment variable takes precedence @appbox_meta_artifact_code = env.nil? ? code : env @appbox_meta_artifact_code end def appbox_territory_code return @appbox_territory_code if @appbox_territory_code @appbox_territory_code = ENV["DTC_RAKE_APPBOX_TERRITORY_CODE"] @appbox_territory_code end def appbox_territory_code=(code) env = ENV["DTC_RAKE_APPBOX_TERRITORY_CODE"] # environment variable takes precedence @appbox_territory_code = env.nil? ? code : env @appbox_territory_code end def appbox_uarchive return @appbox_uarchive if @appbox_uarchive @appbox_uarchive = ENV["DTC_RAKE_APPBOX_UARCHIVE"] @appbox_uarchive end def appbox_uarchive=(path) env = ENV["DTC_RAKE_APPBOX_UARCHIVE"] # environment variable takes precedence path = env unless env.nil? unless path.nil? # File.expand_path converts path to absolute and expands "~" to real user home path path = File.expand_path(path) if Pathname.new(path).relative? @appbox_uarchive = File.join(root_dir, path) else @appbox_uarchive = path end end end def appbox_version return @appbox_version if @appbox_version @appbox_version = ENV["DTC_RAKE_APPBOX_VERSION"] if @appbox_version.nil? version_file = File.join(root_dir, 'VERSION') unless File.file?(version_file) _error "Unknown appbox version. Set it in #{version_file} file or DTC_RAKE_APPBOX_VERSION environment variable." end version = File.binread(version_file).strip @appbox_version = version end end def appbox_version=(code) env = ENV["DTC_RAKE_APPBOX_VERSION"] # environment variable takes precedence @appbox_version = env.nil? ? code : env @appbox_version end def colorize return @colorize if @colorize env = ENV["DTC_RAKE_COLORIZE"] unless env.nil? @colorize = to_bool(env) end @colorize end def colorize=(flag) env = ENV["DTC_RAKE_COLORIZE"] # environment variable takes precedence if env.nil? @colorize = flag else @colorize = to_bool(env) end @colorize end def output_dir return @output_dir if @output_dir @output_dir = ENV["DTC_RAKE_OUTPUT_DIR"] @output_dir end def output_dir=(path) env = ENV["DTC_RAKE_OUTPUT_DIR"] # environment variable takes precedence path = env unless env.nil? unless path.nil? # File.expand_path converts path to absolute and expands "~" to real user home path path = File.expand_path(path) if Pathname.new(path).relative? @output_dir = File.join(root_dir, path) else @output_dir = path end end end def product_code return @product_code if @product_code @product_code = ENV["DTC_RAKE_PRODUCT_CODE"] @product_code end def product_code=(code) env = ENV["DTC_RAKE_PRODUCT_CODE"] # environment variable takes precedence @product_code = env.nil? ? code : env @product_code end def product_name return @product_name if @product_name @product_name = ENV["DTC_RAKE_PRODUCT_NAME"] @product_name end def product_name=(name) env = ENV["DTC_RAKE_PRODUCT_NAME"] # environment variable takes precedence @product_name = env.nil? ? name : env @product_name end def root_dir return @root_dir if @root_dir @root_dir = ENV["DTC_RAKE_ROOT_DIR"] @root_dir = Dir.pwd if @root_dir.nil? @root_dir end def root_dir=(path) env = ENV["DTC_RAKE_ROOT_DIR"] # environment variable takes precedence path = env unless env.nil? unless path.nil? # File.expand_path converts path to absolute and expands "~" to real user home path # If the path is relative, it gets evaluated against to current working directory (Dir.pwd) @root_dir = File.expand_path(path) end end def upload_readme return @upload_readme if @upload_readme @upload_readme = to_bool(ENV["DTC_RAKE_UPLOAD_README"]) @upload_readme end def upload_readme=(flag) env = ENV["DTC_RAKE_UPLOAD_README"] # environment variable takes precedence if env.nil? @upload_readme = flag else @upload_readme = to_bool(env) end @upload_readme end def upload_uucloud_descriptor return @upload_uucloud_descriptor if @upload_uucloud_descriptor @upload_uucloud_descriptor = to_bool(ENV["DTC_RAKE_UPLOAD_UUCLOUD_DESCRIPTOR"]) @upload_uucloud_descriptor end def upload_uucloud_descriptor=(flag) env = ENV["DTC_RAKE_UPLOAD_UUCLOUD_DESCRIPTOR"] # environment variable takes precedence if env.nil? @upload_uucloud_descriptor = flag else @upload_uucloud_descriptor = to_bool(env) end @upload_uucloud_descriptor end def uucloud_descriptor_path return @uucloud_descriptor_path if @uucloud_descriptor_path env = ENV["DTC_RAKE_UUCLOUD_DESCRIPTOR_PATH"] if env.nil? default_file_path = File.expand_path(File.join(root_dir, "uucloud_descriptor.json")) Dir.chdir(root_dir) do unless File.file?(default_file_path) _error "uuCloud deployment descriptor not found. Please create file #{default_file_path} or configure its path in Rakefile." \ " Or you can configure its path in DTC_RAKE_UUCLOUD_DESCRIPTOR_PATH environment variable." end @uucloud_descriptor_path = default_file_path end else env = File.expand_path(env) unless File.file?(env) _error "uuCloud deployment descriptor not found. Please create file #{env} or configure its path in" \ " DTC_RAKE_UUCLOUD_DESCRIPTOR_PATH environment variable." end @uucloud_descriptor_path = env end end def vendor=(name) @vendor = name.downcase unless name.nil? end def version_files=(list) if list.nil? @version_files = [] elsif list.is_a?(Array) @version_files = list else _error "Invalid version_files value - #{list} (#{list})." \ " Use an array of strings (representing file names or glob patterns)." end end private def to_bool(value) (value =~ /^true|1|yes$/i) == 0 end def guess_vendor_and_app env_vendor = ENV["DTC_RAKE_VENDOR"] env_app = ENV["DTC_RAKE_APP"] if env_vendor.nil? || env_app.nil? base_name = File.basename(root_dir) if /([^_]+)_([^-]+)-appbox/ =~ base_name @vendor = env_vendor.nil? ? $1 : env_vendor @app = env_app.nil? ? $2 : env_app else _error "Appbox root folder (#{base_name}) does not match naming convention _-appbox." \ " Either change the folder name or configure 'vendor' and 'app' within DtcRake.configure in Rakefile." \ " Or you can set DTC_RAKE_VENDOR and DTC_RAKE_APP environment variables to specify those." end else @vendor = env_vendor @app = env_app end end def _error(msg) # this avoids cycle between dtc_rake/config and dtc_rake/ui require "dtc_rake/ui" self.class.send(:include, DtcRake::UI) unless self.class.include?(DtcRake::UI) error(msg) end end end