#!/usr/bin/env ruby # frozen_string_literal: true $LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib') require 'thor' require 'leap_salesforce/error' require 'leap_salesforce/parameters' require 'leap_salesforce/generator/generator' require 'leap_salesforce/version' require 'colorize' module LeapSalesforce # Executable for setting up Leap Salesforce class Exe < Thor include LeapSalesforce::Generator include LeapSalesforce::ExeHelpers OAUTH_WIKI = 'https://gitlab.com/leap-dojo/leap_salesforce/wikis/SetUpOAuthApp' SFDX_WIKI = 'https://gitlab.com/leap-dojo/leap_salesforce/wikis/UsingSfdxAuth' desc 'init', 'Create new leap salesforce repository' option :setup_done, default: false, type: :boolean, banner: 'Whether setup is done' option :client_id, banner: 'OAuth2 client id' option :client_secret, banner: 'OAuth2 client secret' option :username, banner: 'Username' option :user_key, banner: 'Key to identify user by' option :password, banner: 'User password' option :environment, banner: 'Environment to set automation code up for' option :sfdx, banner: 'Whether to use sfdx for authentication', default: false, type: :boolean def init LeapSalesforce.sfdx = options[:sfdx] wiki = LeapSalesforce.sfdx ? SFDX_WIKI : OAUTH_WIKI unless options[:setup_done] # Ask user to create OAuth application by following doc oauth_setup = input_for "Have you set up OAuth application by following #{wiki} (y/n)?", :red exit if oauth_setup.downcase == 'n' end query_for_parameters # TODO: for sfdx has_ignore_file = File.exist?('.gitignore') generate_files binding, ['Gemfile', 'Rakefile', '.leap_salesforce.yml', '.rspec', '.gitignore', { config: ['general.rb', { credentials: 'salesforce_oauth2.yml' }] }, { spec: %w[spec_helper.rb limit_spec.rb crud_eg_spec.rb picklists_spec.rb] }] environment_content = read_template File.join('config', 'environments', 'specific_file.rb.erb'), binding generate_file File.join('config', 'environments', "#{LeapSalesforce.environment}.rb"), environment_content, overwrite: false puts "\u2713 Initial files created!".colorize :green puts 'Installing gems'.colorize :yellow puts `bundle install` puts 'Creating initial Soql objects. See ".leap_salesforce.yml" for list'.colorize :yellow puts `rake leaps:create_soql_objects` puts 'Creating enums'.colorize :yellow puts `rake leaps:create_enums` return unless has_ignore_file puts 'Your .gitignore is already present so not updated. Please ignore "config/credentials/" and "logs/"'.colorize :red end desc 'create_soql_objects', 'Creates SOQL objects from metadata based on .leap_salesforce.yml' option :language def create_soql_objects LeapSalesforce.language = options[:language] if options[:language] require 'leap_salesforce/generator/soql_objects' LeapSalesforce.oauth_working? LeapSalesforce::Users.execute_as_if_present key: :admin do LeapSalesforce::Generator::SoqlObjects.new.create_all end end desc 'version', 'Version this library' def version puts LeapSalesforce::VERSION end def self.exit_on_failure? true end end end LeapSalesforce::Exe.start(ARGV)