PageRenderTime 48ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/swiss

https://bitbucket.org/peddamat/swiss
Ruby | 173 lines | 157 code | 14 blank | 2 comment | 2 complexity | 0fc75a97c3ea895e98fded9d78426252 MD5 | raw file
  1. #!/usr/bin/ruby
  2. $LOAD_PATH << './lib'
  3. require 'rubygems'
  4. require 'thor'
  5. require 'configuration'
  6. require 'lib/initialize'
  7. require 'lib/import'
  8. require 'lib/export'
  9. require 'lib/staging'
  10. require 'ruby-debug'
  11. # This method is included in the Rails Env., so we have to define it here...
  12. class String
  13. def starts_with?(prefix)
  14. prefix = prefix.to_s
  15. self[0, prefix.length] == prefix
  16. end
  17. end
  18. class Swiss < Thor
  19. no_tasks do
  20. def load_config(project_name, project_type)
  21. require 'rubygems'
  22. require 'json'
  23. file = File.open('config.json', 'r')
  24. json = file.readlines.to_s
  25. @settings = JSON.parse(json)
  26. @settings['project_name'] = project_name
  27. @settings['project_type'] = project_type
  28. end
  29. def append_timestamp(project_name)
  30. require 'date'
  31. date = DateTime.now.strftime("%H%M%S")
  32. sprintf('%s_%s', project_name, date).strip.downcase.gsub(' ','_')
  33. end
  34. def get_project_type(project_name)
  35. match = Dir.glob("repos/**/#{project_name}").first
  36. unless match.nil?
  37. match.split('/')[1]
  38. else
  39. throw
  40. end
  41. end
  42. end
  43. desc "configure", "Check the configuration"
  44. def configure
  45. if !File.exists?("config.rb")
  46. print "** Hey! It looks like this is your first time running swiss.\n"
  47. print "** Before we get started, you'll need to create a config.ru.\n"
  48. else
  49. Kernel.load 'config.rb'
  50. c = Configuration.for 'swiss'
  51. print "\n"
  52. print "---\n"
  53. print "*\n"
  54. print "* Welcome to swiss!\n"
  55. print "*\n"
  56. print "* - A project management system in a directory."
  57. print "\n\n"
  58. help
  59. end
  60. end
  61. desc "add [PROJECT_NAME]", "Add a new project"
  62. method_option :type, :aliases => "-t", :required => true, :desc => "The type of project to add"
  63. def add(project_name)
  64. load_config append_timestamp(project_name), options[:type]
  65. init = SwissLib::Initialize.new @settings
  66. project_path = init.initialize_project
  67. project_path = File.expand_path(project_path)
  68. new_project_name = File.basename project_path
  69. print "\n"
  70. print "*** Project \"#{project_name}\" initialized!\n"
  71. print "** Name : #{new_project_name}\n"
  72. print "** Local URL : #{File.join "http://localhost", new_project_name}\n"
  73. print "** Staging URL: #{File.join "http://staging.talentgurus.net", new_project_name}\n"
  74. print "** System Path: #{project_path}\n"
  75. print "** Path copied to clipboard.\n\n"
  76. `echo #{project_path} | pbcopy`
  77. end
  78. desc "export [PROJECT_NAME]", "Export a project"
  79. def export(project_name)
  80. load_config project_name, get_project_type(project_name)
  81. export = SwissLib::Export.new @settings
  82. project_path = export.export_project "new_host", "new_mysql_db", "new_mysql_user", "new_mysql_pass", "new_db_host"
  83. require 'fileutils'
  84. files = []
  85. Dir.glob(File.join(project_path, "zips", "*.zip")) do |f|
  86. FileUtils.mv(f, "zips")
  87. files << f
  88. end
  89. # Cleanup temporary directory
  90. FileUtils.rm_rf(project_path)
  91. print "\n"
  92. print "*** Project successfully exported!\n"
  93. files.each { |f| print "** zips/" + File.basename(f) + "\n" }
  94. end
  95. desc "import [PROJECT_NAME]", "Import a project"
  96. method_option :file, :aliases => "-f", :required => true, :desc => "Project archive"
  97. def import(project_name)
  98. load_config project_name, get_project_type(project_name)
  99. import = SwissLib::Import.new @settings
  100. import.import_project_from_zip options[:file]
  101. end
  102. desc "list", "List available projects"
  103. def list
  104. types = Dir.glob('repos/*')
  105. types.each do |type|
  106. print "\n"
  107. print "* #{File.basename(type).capitalize} Projects\n\n"
  108. projects = Dir.glob(File.join(type, '*'))
  109. projects.each { |p| print "- #{File.basename(p)}\n" }
  110. end
  111. end
  112. desc "stage [PROJECT_NAME]", "Deploy a project to staging"
  113. def stage(project_name)
  114. load_config project_name, get_project_type(project_name)
  115. woo = SwissLib::Staging.new @settings
  116. woo.update_staging
  117. end
  118. desc "update", "Update the deployment scripts for all projects"
  119. def update
  120. end
  121. desc "bootstrap", "Bootstrap swiss"
  122. def bootstrap
  123. FileUtils.makedirs('base/tooling') if !File.exists?('base/tooling')
  124. FileUtils.makedirs('base/projects') if !File.exists?('base/projects')
  125. if !File.exists?('base/tooling/eclipse')
  126. `cd base/tooling && hg clone https://bitbucket.org/peddamat/swiss-eclipse-tooling eclipse`
  127. end
  128. if !File.exists?('base/projects/wordpress')
  129. `cd base/projects && git clone https://github.com/peddamat/swiss-wordpress-bootstrap wordpress`
  130. `cd base/projects/wordpress && rm -Rf .git`
  131. `cd base/projects/wordpress && hg init && hg add && hg commit -m "Initial commit" -u www-data`
  132. end
  133. end
  134. end
  135. Swiss.default_task("configure")
  136. Swiss.start