lib/bolt/project_manager.rb
# Creates a new project at the specified directory.
#
Creates a new project at the specified directory.
Creates a new project at the specified directory .
def create(path, name, modules)
      require_relative '../bolt/module_installer'
      project       = Pathname.new(File.expand_path(path))
      old_config    = project + 'bolt.yaml'
      config        = project + 'bolt-project.yaml'
      puppetfile    = project + 'Puppetfile'
      moduledir     = project + '.modules'
      inventoryfile = project + 'inventory.yaml'
      gitignore     = project + '.gitignore'
      project_name  = name || File.basename(project)
      if config.exist?
        if modules
          command = Bolt::Util.powershell? ? 'Add-BoltModule -Module' : 'bolt module add'
          raise Bolt::Error.new(
            "Found existing project directory with #{config.basename} at #{project}, "\
            "unable to initialize project with modules. To add modules to the project, "\
            "run '#{command} <module>' instead.",
            'bolt/existing-project-error'
          )
        else
          raise Bolt::Error.new(
            "Found existing project directory with #{config.basename} at #{project}, "\
            "unable to initialize project.",
            'bolt/existing-project-error'
          )
        end
      elsif old_config.exist?
        command = Bolt::Util.powershell? ? 'Update-BoltProject' : 'bolt project migrate'
        raise Bolt::Error.new(
          "Found existing project directory with #{old_config.basename} at #{project}, "\
          "unable to initialize project. #{old_config.basename} is deprecated. To "\
          "update the project to current best practices, run '#{command}'.",
          'bolt/existing-project-error'
        )
      elsif modules && puppetfile.exist?
        raise Bolt::Error.new(
          "Found existing Puppetfile at #{puppetfile}, unable to initialize project "\
          "with modules.",
          'bolt/existing-puppetfile-error'
        )
      elsif project_name !~ Bolt::Module::MODULE_NAME_REGEX
        if name
          raise Bolt::ValidationError,
                "The provided project name '#{project_name}' is invalid; project name must "\
                "begin with a lowercase letter and can include lowercase letters, "\
                "numbers, and underscores."
        else
          command = Bolt::Util.powershell? ? 'New-BoltProject -Name' : 'bolt project init'
          raise Bolt::ValidationError,
                "The current directory name '#{project_name}' is an invalid project name. "\
                "Please specify a name using '#{command} <name>'."
        end
      end
      if modules
        @outputter.start_spin
        Bolt::ModuleInstaller.new(@outputter, @pal).install(modules, puppetfile, moduledir)
        @outputter.stop_spin
      end
      data = { 'name' => project_name }
      data['modules'] = modules || []
      begin
        File.write(config.to_path, data.to_yaml)
      rescue StandardError => e
        raise Bolt::FileError.new("Could not create bolt-project.yaml at #{project}: #{e.message}", nil)
      end
      unless inventoryfile.exist?
        begin
          File.write(inventoryfile.to_path, INVENTORY_TEMPLATE)
        rescue StandardError => e
          raise Bolt::FileError.new("Could not create inventory.yaml at #{project}: #{e.message}", nil)
        end
      end
      unless gitignore.exist?
        begin
          File.write(gitignore.to_path, GITIGNORE_CONTENT)
        rescue StandardError => e
          raise Bolt::FileError.new("Could not create .gitignore at #{project}: #{e.message}", nil)
        end
      end
      @outputter.print_message("Successfully created Bolt project at #{project}")
      0
    end
def create ( path , name , modules ) require_relative '../bolt/module_installer' project = Pathname . new ( File . expand_path ( path ) ) old_config = project + 'bolt.yaml' config = project + 'bolt-project.yaml' puppetfile = project + 'Puppetfile' moduledir = project + '.modules' inventoryfile = project + 'inventory.yaml' gitignore = project + '.gitignore' project_name = name || File . basename ( project ) if config . exist? if modules command = Bolt :: Util . powershell? ? 'Add-BoltModule -Module' : 'bolt module add' raise Bolt :: Error . new ( "Found existing project directory with #{config.basename} at #{project}, " "unable to initialize project with modules. To add modules to the project, " "run '#{command} <module>' instead." , 'bolt/existing-project-error' ) else raise Bolt :: Error . new ( "Found existing project directory with #{config.basename} at #{project}, " "unable to initialize project." , 'bolt/existing-project-error' ) end elsif old_config . exist? command = Bolt :: Util . powershell? ? 'Update-BoltProject' : 'bolt project migrate' raise Bolt :: Error . new ( "Found existing project directory with #{old_config.basename} at #{project}, " "unable to initialize project. #{old_config.basename} is deprecated. To " "update the project to current best practices, run '#{command}'." , 'bolt/existing-project-error' ) elsif modules && puppetfile . exist? raise Bolt :: Error . new ( "Found existing Puppetfile at #{puppetfile}, unable to initialize project " "with modules." , 'bolt/existing-puppetfile-error' ) elsif project_name !~ Bolt :: Module :: MODULE_NAME_REGEX if name raise Bolt :: ValidationError , "The provided project name '#{project_name}' is invalid; project name must " "begin with a lowercase letter and can include lowercase letters, " "numbers, and underscores." else command = Bolt :: Util . powershell? ? 'New-BoltProject -Name' : 'bolt project init' raise Bolt :: ValidationError , "The current directory name '#{project_name}' is an invalid project name. " "Please specify a name using '#{command} <name>'." end end if modules @outputter . start_spin Bolt :: ModuleInstaller . new ( @outputter , @pal ) . install ( modules , puppetfile , moduledir ) @outputter . stop_spin end data = { 'name' => project_name } data [ 'modules' ] = modules || [ ] begin File . write ( config . to_path , data . to_yaml ) rescue StandardError => e raise Bolt :: FileError . new ( "Could not create bolt-project.yaml at #{project}: #{e.message}" , nil ) end unless inventoryfile . exist? begin File . write ( inventoryfile . to_path , INVENTORY_TEMPLATE ) rescue StandardError => e raise Bolt :: FileError . new ( "Could not create inventory.yaml at #{project}: #{e.message}" , nil ) end end unless gitignore . exist? begin File . write ( gitignore . to_path , GITIGNORE_CONTENT ) rescue StandardError => e raise Bolt :: FileError . new ( "Could not create .gitignore at #{project}: #{e.message}" , nil ) end end @outputter . print_message ( "Successfully created Bolt project at #{project}" ) 0 end
Creates a new project at the specified directory.
Creates a new project at the specified directory .
# If modules were specified, resolve and install first. We want to error # early here and not initialize the project if the modules cannot be # resolved and installed.
path
<not_specific>
name
<not_specific>
modules
<not_specific>