/swiss
Ruby | 173 lines | 157 code | 14 blank | 2 comment | 2 complexity | 0fc75a97c3ea895e98fded9d78426252 MD5 | raw file
- #!/usr/bin/ruby
- $LOAD_PATH << './lib'
- require 'rubygems'
- require 'thor'
- require 'configuration'
- require 'lib/initialize'
- require 'lib/import'
- require 'lib/export'
- require 'lib/staging'
- require 'ruby-debug'
- # This method is included in the Rails Env., so we have to define it here...
- class String
- def starts_with?(prefix)
- prefix = prefix.to_s
- self[0, prefix.length] == prefix
- end
- end
- class Swiss < Thor
- no_tasks do
- def load_config(project_name, project_type)
- require 'rubygems'
- require 'json'
- file = File.open('config.json', 'r')
- json = file.readlines.to_s
- @settings = JSON.parse(json)
- @settings['project_name'] = project_name
- @settings['project_type'] = project_type
- end
- def append_timestamp(project_name)
- require 'date'
- date = DateTime.now.strftime("%H%M%S")
- sprintf('%s_%s', project_name, date).strip.downcase.gsub(' ','_')
- end
- def get_project_type(project_name)
- match = Dir.glob("repos/**/#{project_name}").first
- unless match.nil?
- match.split('/')[1]
- else
- throw
- end
- end
- end
- desc "configure", "Check the configuration"
- def configure
- if !File.exists?("config.rb")
- print "** Hey! It looks like this is your first time running swiss.\n"
- print "** Before we get started, you'll need to create a config.ru.\n"
- else
- Kernel.load 'config.rb'
- c = Configuration.for 'swiss'
- print "\n"
- print "---\n"
- print "*\n"
- print "* Welcome to swiss!\n"
- print "*\n"
- print "* - A project management system in a directory."
- print "\n\n"
- help
- end
- end
- desc "add [PROJECT_NAME]", "Add a new project"
- method_option :type, :aliases => "-t", :required => true, :desc => "The type of project to add"
- def add(project_name)
- load_config append_timestamp(project_name), options[:type]
- init = SwissLib::Initialize.new @settings
- project_path = init.initialize_project
- project_path = File.expand_path(project_path)
- new_project_name = File.basename project_path
- print "\n"
- print "*** Project \"#{project_name}\" initialized!\n"
- print "** Name : #{new_project_name}\n"
- print "** Local URL : #{File.join "http://localhost", new_project_name}\n"
- print "** Staging URL: #{File.join "http://staging.talentgurus.net", new_project_name}\n"
- print "** System Path: #{project_path}\n"
- print "** Path copied to clipboard.\n\n"
- `echo #{project_path} | pbcopy`
- end
- desc "export [PROJECT_NAME]", "Export a project"
- def export(project_name)
- load_config project_name, get_project_type(project_name)
- export = SwissLib::Export.new @settings
- project_path = export.export_project "new_host", "new_mysql_db", "new_mysql_user", "new_mysql_pass", "new_db_host"
- require 'fileutils'
- files = []
- Dir.glob(File.join(project_path, "zips", "*.zip")) do |f|
- FileUtils.mv(f, "zips")
- files << f
- end
- # Cleanup temporary directory
- FileUtils.rm_rf(project_path)
- print "\n"
- print "*** Project successfully exported!\n"
- files.each { |f| print "** zips/" + File.basename(f) + "\n" }
- end
- desc "import [PROJECT_NAME]", "Import a project"
- method_option :file, :aliases => "-f", :required => true, :desc => "Project archive"
- def import(project_name)
- load_config project_name, get_project_type(project_name)
- import = SwissLib::Import.new @settings
- import.import_project_from_zip options[:file]
- end
- desc "list", "List available projects"
- def list
- types = Dir.glob('repos/*')
- types.each do |type|
- print "\n"
- print "* #{File.basename(type).capitalize} Projects\n\n"
- projects = Dir.glob(File.join(type, '*'))
- projects.each { |p| print "- #{File.basename(p)}\n" }
- end
- end
- desc "stage [PROJECT_NAME]", "Deploy a project to staging"
- def stage(project_name)
- load_config project_name, get_project_type(project_name)
- woo = SwissLib::Staging.new @settings
- woo.update_staging
- end
- desc "update", "Update the deployment scripts for all projects"
- def update
- end
- desc "bootstrap", "Bootstrap swiss"
- def bootstrap
- FileUtils.makedirs('base/tooling') if !File.exists?('base/tooling')
- FileUtils.makedirs('base/projects') if !File.exists?('base/projects')
- if !File.exists?('base/tooling/eclipse')
- `cd base/tooling && hg clone https://bitbucket.org/peddamat/swiss-eclipse-tooling eclipse`
- end
- if !File.exists?('base/projects/wordpress')
- `cd base/projects && git clone https://github.com/peddamat/swiss-wordpress-bootstrap wordpress`
- `cd base/projects/wordpress && rm -Rf .git`
- `cd base/projects/wordpress && hg init && hg add && hg commit -m "Initial commit" -u www-data`
- end
- end
- end
- Swiss.default_task("configure")
- Swiss.start