class Sumodev::Commands::Project

Public Instance Methods

change_location(destination) click to toggle source
# File lib/sumodev/commands/project.rb, line 375
def change_location(destination)
  say colorize("Your project is located in: *#{destination}*"), :green
end
clone_repo(repo, destination) click to toggle source
# File lib/sumodev/commands/project.rb, line 189
def clone_repo(repo, destination)
  run_command_without_output(
    "rm -rf #{destination}; git clone #{repo} #{destination}",
    nil,
    "--> Cloning repo #{repo}"
  )
end
deploy(path, stage) click to toggle source
# File lib/sumodev/commands/project.rb, line 364
def deploy(path, stage)
  run_command_without_output(
    [
      "grunt build",
      "bundle exec cap #{stage} deploy"
    ],
    path,
    "--> Deploying to #{stage}"
  )
end
fetch_data(path, stage) click to toggle source
# File lib/sumodev/commands/project.rb, line 306
def fetch_data(path, stage)
  say "Grabbing data", :green

  if File.file?("#{path}/Gemfile")
    system "cd #{path}; bundle exec cap #{stage} sumodev:db:get"
    system "cd #{path}; bundle exec cap #{stage} sumodev:files:get"
  else
    system "cd #{path}; cap #{stage} sumodev:db:get"
    system "cd #{path}; cap #{stage} sumodev:files:get"
  end
end
get(repo, stage="staging") click to toggle source
# File lib/sumodev/commands/project.rb, line 20
def get(repo, stage="staging")
  begin
    tmp_path = File.expand_path(Sumodev::Config.get('SUMO_TEMP_PATH'));
    capfile_path = "#{tmp_path}/temp_project/Capfile"
    sites_path = File.expand_path(Sumodev::Config.get('SUMO_SITES_PATH'))

    clone_repo(repo, "#{tmp_path}/temp_project")

    if options[:project_path].nil?
      client, project = process_capfile(capfile_path)
      project_path = File.expand_path("#{sites_path}/#{client}/#{project}")
    else
      project_path = File.expand_path(options[:project_path])
    end

    move_project("#{tmp_path}/temp_project", project_path)

    if !options[:only_clone]
      install_bundles(project_path) if options[:bundles]
      install_node_modules(project_path) if options[:node_modules]
      install_bower_packages(project_path) if options[:bower]
      install_composer_packages(project_path, options[:composer_run_scripts]) if options[:composer]
      fetch_data(project_path, stage) if options[:fetch_data]
      mark_as_installed(project_path) if options[:mark_as_installed]
    end

    change_location(project_path)
    say "All done", :green
  rescue Exception => e
    say e.message, :red
  end
end
get_client(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 169
def get_client(path)
  get_var(path, "client")
end
get_project(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 173
def get_project(path)
  get_var(path, "project")
end
get_var(path, field) click to toggle source
# File lib/sumodev/commands/project.rb, line 177
def get_var(path, field)
  File.readlines(path).each do |line|
    # match = Regexp.new(":''#{field},\s*\"(.*?)\"").match(line)
    match = Regexp.new(":#{field},\s*[\"|\'](.*?)[\"|\']").match(line)
    if match != nil && match.length > 0
      return match[1]
    end
  end

  raise "Field #{field} not found"
end
initial_deploy(path, client, project, database_name, database_user, database_password, errbit_api_key) click to toggle source
# File lib/sumodev/commands/project.rb, line 339
def initial_deploy(path, client, project, database_name, database_user, database_password, errbit_api_key)
  content = File.read("#{path}/app/config/parameters.yml")
  content = content
    .gsub(/database.host:.*/, "database.host: 127.0.0.1")
    .gsub(/database.name:.*/, "database.name: #{database_name}")
    .gsub(/database.user:.*/, "database.user: #{database_user}")
    .gsub(/database.password:.*/, "database.password: #{database_password}")
    .gsub(/site.domain:.*/, "site.domain: #{project}.#{client}.sumocoders.eu")
    .gsub(/sumo.errbit_api_key:.*/, "sumo.errbit_api_key: #{errbit_api_key}")
  File.write("#{path}/app/config/parameters.dev.yml", content)

  run_command_without_output(
    [
      "bundle exec cap deploy:setup",
      "bundle exec cap deploy",
      "bundle exec cap sumodev:db:put",
      "bundle exec cap sumodev:db:lock",
      "scp app/config/parameters.dev.yml sites@dev02.sumocoders.eu:/home/sites/apps/#{client}/#{project}/shared/config/parameters.yml",
      "rm #{path}/app/config/parameters.dev.yml"
    ],
    path,
    "--> Running initial deploy, aka deploy:setup"
  )
end
initialize_repo(path, repo) click to toggle source
# File lib/sumodev/commands/project.rb, line 197
def initialize_repo(path, repo)
  run_command_without_output(
    [
      "git init",
      "git add .",
      "git commit -n -m 'Initial commit'",
      "git remote add origin #{repo}",
      "git push -u origin master",
      "git checkout -b staging",
      "git push -u origin staging"
    ],
    path,
    "--> Initialize repo #{repo}"
  )
end
install_bower_packages(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 264
def install_bower_packages(path)
  return unless File.file?("#{path}/bower.json")

  run_command_without_output("bower install", path, "--> Installing bower packages")
end
install_bundles(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 247
def install_bundles(path)
  return unless File.file?("#{path}/Gemfile")

  run_command_without_output("bundle install", path, "--> Installing gems")
end
install_composer_packages(path, run_scripts) click to toggle source
# File lib/sumodev/commands/project.rb, line 270
def install_composer_packages(path, run_scripts)
  return unless File.file?("#{path}/composer.json")

  cmd = "composer install"
  cmd = "#{cmd} --no-scripts" unless run_scripts

  run_command_without_output(cmd, path, "--> Installing composer dependencies")
end
install_initial_theme(path, database) click to toggle source
# File lib/sumodev/commands/project.rb, line 279
def install_initial_theme(path, database)
  run_command_without_output(
    [
      "php tools/install_locale.php -f src/Frontend/Themes/Bootstrap/locale.xml",
      "cp -r src/Frontend/Themes/Bootstrap src/Frontend/Themes/Custom",
      "sed -i '' -e 's|.*<name>bootstrap</name>.*|  <name>custom</name>|g' src/Frontend/Themes/Custom/info.xml",
      "sed -i '' -e 's/set :theme.*/set :theme, \"Custom\"/g' Capfile",
      "sed -i '' -e 's/.*\"theme\":.*/  \"theme\": \"Custom\",/g' package.json"
    ],
    path,
    "--> Creating the initial Custom theme"
  )
end
install_node_modules(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 253
def install_node_modules(path)
  return unless File.file?("#{path}/package.json")

  run_command_without_output(
    [
        "yarn install",
    ],
    path,
    "--> Installing node modules")
end
mark_as_installed(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 318
def mark_as_installed(path)
  return unless File.file?("#{path}/VERSION.md")

  content = File.read("#{path}/VERSION.md")
  min_version = Gem::Version.new('3.6')
  project_version = Gem::Version.new(content)

  if project_version > min_version
    if project_version < Gem::Version.new('3.7')
      file_path = "/install/cache/installed.txt"
    elsif project_version <= Gem::Version.new('3.8')
      file_path = "/src/Install/Cache/installed.txt"
    end

    if file_path
      say "Creating installed.txt", :green
      system "touch #{path}#{file_path}"
    end
  end
end
move_project(source, destination) click to toggle source
# File lib/sumodev/commands/project.rb, line 293
def move_project(source, destination)
  raise "Project folder already exists." if File.directory?(destination)

  run_command_without_output(
    [
      "mkdir -p #{destination}",
      "shopt -s dotglob; mv #{source}/* #{destination}"
    ],
    nil,
    "--> Moving everything into place"
  )
end
new(type, client, project) click to toggle source
# File lib/sumodev/commands/project.rb, line 61
def new(type, client, project)
  begin
    if type != "fork"
      raise "Only fork is implemented"
    end

    repo = "git@git.sumocoders.be:sumocoders/#{client}-#{project}.git"
    repo_url = repo.gsub('.be:', '.be/').gsub('git@', 'http://').gsub('.git', '')

    # check if repo exists
    begin
      self.run_command_without_output("git ls-remote #{repo}", nil, "--> Check if repo #{repo} exists.")
    rescue Exception => e
      raise "Please create a repository named #{client}-#{project} first:\nhttp://git.sumocoders.be/projects/new"
    end

    tmp_path = File.expand_path(Sumodev::Config.get('SUMO_TEMP_PATH'));
    sites_path = File.expand_path(Sumodev::Config.get('SUMO_SITES_PATH'))

    if options[:project_path].nil?
      project_path = File.expand_path("#{sites_path}/#{client}/#{project}")
    else
      project_path = File.expand_path(options[:project_path])
    end

    clone_repo("git://github.com/sumocoders/forkcms.git", "#{tmp_path}/temp_project")
    run_command_without_output("rm -rf #{tmp_path}/temp_project/.git")
    initialize_repo("#{tmp_path}/temp_project", repo)

    # ask to set the default branch to staging
    run_command_without_output("open #{repo_url}/edit")
    continue?("Gitlab will now open, change the default branch to *staging*")

    # ask to add a new app in Errbit and get the errbit_api_key
    run_command_without_output("open https://errors.sumocoders.be/apps/new")
    errbit_api_key = ask("Errbit will now open, enter the Errbit API key:")

    # move it into place
    move_project("#{tmp_path}/temp_project", project_path)
    populate_capfile("#{project_path}/Capfile", client, project, repo, errbit_api_key)

    # install assets
    install_bundles(project_path) if options[:bundles]
    install_node_modules(project_path) if options[:node_modules]
    install_bower_packages(project_path) if options[:bower]
    install_composer_packages(project_path, options[:composer_run_scripts]) if options[:composer]

    # create database
    run_command_without_output(
      [
        "bundle exec cap staging sumodev:db:create",
        "bundle exec cap staging sumodev:db:get"
      ],
      project_path,
      "--> Creating database on staging and local"
    )

    # grab database information
    output = run_command_without_output("bundle exec cap staging sumodev:db:info 2>&1", project_path)
    database_information = output.out
      .gsub("** [out :: dev.sumocoders.be] ", "")
      .gsub("\n", "")

    database_name = database_information.scan(/database:(.*)user/)[0][0].gsub(/\s+/, '')
    database_user = database_information.scan(/user:(.*)pass/)[0][0].gsub(/\s+/, '')
    database_password = database_information.scan(/pass:(.*)command/)[0][0].gsub(/\s+/, '')

    run_command_without_output(
      "sed -i '' -e 's|site.path_www:.*|site.path_www: %kernel.root_dir%/..|g' #{project_path}/app/config/parameters_install.yml",
      nil,
      "--> Configuring parameters_install.yml"
    )
    run_command_without_output("rm -rf #{project_path}/app/cache/install")

    # run the installer
    run_command_without_output("open http://#{project}.#{client}.dev")
    continue?("The installer will now open.\nYou can use *#{database_name}* as the database_name.")

    install_initial_theme(project_path, database_name)

    # commit our changes
    run_command_without_output(
      [
        "git add Capfile",
        "git add app/config/parameters_install.yml",
        "git add package.json",
        "git add yarn.lock",
        "git add Gemfile.lock",
        "git add src/Frontend/Themes/Custom",
        "git commit -n -m 'Init configuration of the project'",
        "git push"
      ],
      project_path,
      "--> Commiting the initial configuration of the project"
    )

    # deploy
    initial_deploy(project_path, client, project, database_name, database_user, database_password, errbit_api_key)
    deploy(project_path, "staging")

    change_location(project_path)
    say "All done", :green
  rescue Exception => e
    say e.message, :red
  end
end
populate_capfile(path, client, project, repo, errbit_api_key) click to toggle source
# File lib/sumodev/commands/project.rb, line 231
def populate_capfile(path, client, project, repo, errbit_api_key)
  unless File.file?(path)
    raise "No Capfile found"
  end

  say "--> Configuring Capfile", :green

  content = File.read(path)
    .gsub(/set :client,.*/, "set :client, \"#{client}\"")
    .gsub(/set :project,.*/, "set :project, \"#{project}\"")
    .gsub(/set :repository,.*/, "set :repository, \"#{repo}\"")
    .gsub(/set :production_errbit_api_key,.*/, "set :production_errbit_api_key, \"#{errbit_api_key}\"")

  File.write(path, content)
end
process_capfile(path) click to toggle source
# File lib/sumodev/commands/project.rb, line 213
def process_capfile(path)
  unless File.file?(path)
    raise "No Capfile found"
  end

  content = File.read(path)

  unless content.include?("set :client")
    raise "No client found in the Capfile"
  end

  unless content.include?("set :project")
    raise "No project found in the Capfile"
  end

  [get_client(path), get_project(path)]
end