Class: ShopifyCLI::Project

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/shopify_cli/project.rb

Overview

ShopifyCLI::Project captures the current project that the user is working on. This class can be used to fetch and save project environment as well as the project config ‘.shopify-cli.yml`.

Direct Known Subclasses

Extension::ExtensionProject

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.at(dir) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/shopify_cli/project.rb', line 110

def at(dir)
  proj_dir = directory(dir)
  if !proj_dir && !ShopifyCLI::Environment.run_as_subprocess?
    raise(ShopifyCLI::Abort, Context.message("core.project.error.not_in_project"))
  end
  @at ||= Hash.new { |h, k| h[k] = new(directory: k) }
  @at[proj_dir]
end

.clearObject



105
106
107
108
# File 'lib/shopify_cli/project.rb', line 105

def clear
  @at = nil
  @dir = nil
end

.current(force_reload: false) ⇒ Object

will get an instance of the project that the user is currently operating on. This is used for access to project resources.

#### Parameters

  • ‘force_reload` - whether to force a reload of the project files

#### Returns

  • ‘project` - a Project instance if the user is currently in the project.

#### Raises

  • ‘ShopifyCLI::Abort` - If the cli is not currently in a project directory then this will be raised with a message implying that the user is not in a project directory.

#### Example

project = ShopifyCLI::Project.current


36
37
38
39
# File 'lib/shopify_cli/project.rb', line 36

def current(force_reload: false)
  clear if force_reload
  at(Dir.pwd)
end

.current_project_typeObject

will fetch the project type of the current project. This is mostly used for internal project type loading, you should not normally need this.

#### Returns

  • ‘type` - a symbol of the name of the project type identifier. i.e. [rails, node] This will be nil if the user is not in a current project.

#### Example

type = ShopifyCLI::Project.current_project_type


65
66
67
68
# File 'lib/shopify_cli/project.rb', line 65

def current_project_type
  return unless has_current?
  current.config["project_type"].to_sym
end

.has_current?Boolean

will return true if the command line is currently within a project

#### Returns

  • ‘has_current?` - boolean, true if there is a current project

Returns:

  • (Boolean)


48
49
50
# File 'lib/shopify_cli/project.rb', line 48

def has_current?
  !directory(Dir.pwd).nil?
end

.project_nameObject



101
102
103
# File 'lib/shopify_cli/project.rb', line 101

def project_name
  File.basename(current.directory)
end

.write(ctx, project_type:, organization_id:, **identifiers) ⇒ Object

writes out the ‘.shopify-cli.yml` file. You should use this when creating a project type so that the rest of your project type commands will load in this project, in the future.

#### Parameters

  • ‘ctx` - the current running context of your command

  • ‘project_type` - a string or symbol of your project type name

  • ‘organization_id` - the id of the partner organization that the app is owned by. Used for metrics

  • ‘identifiers` - an optional hash of other app identifiers

#### Example

ShopifyCLI::Project.write(
  @ctx,
  project_type: "node",
  organization_id: form_data.organization_id,
)


90
91
92
93
94
95
96
97
98
99
# File 'lib/shopify_cli/project.rb', line 90

def write(ctx, project_type:, organization_id:, **identifiers)
  require "yaml" # takes 20ms, so deferred as late as possible.
  content = Hash[{ project_type: project_type, organization_id: organization_id.to_i }
    .merge(identifiers)
    .collect { |k, v| [k.to_s, v] }]
  content["shopify_organization"] = true if Shopifolk.acting_as_shopify_organization?

  ctx.write(".shopify-cli.yml", YAML.dump(content))
  clear
end

Instance Method Details

#configObject

will read, parse and return the .shopify-cli.yml for the project

#### Returns

  • ‘config` - A hash of configuration

#### Raises

  • ‘ShopifyCLI::Abort` - If the yml is invalid or poorly formatted

  • ‘ShopifyCLI::Abort` - If the yml file does not exist

#### Example

ShopifyCLI::Project.current.config


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/shopify_cli/project.rb', line 165

def config
  @config ||= begin
    config = load_yaml_file(".shopify-cli.yml")
    unless config.is_a?(Hash)
      raise ShopifyCLI::Abort, Context.message("core.yaml.error.not_hash", ".shopify-cli.yml")
    end

    # The app_type key was deprecated in favour of project_type, so replace it
    if config.key?("app_type")
      config["project_type"] = config["app_type"]
      config.delete("app_type")
    end

    config
  end
end

#envObject

will read, parse and return the envfile for the project

#### Returns

  • ‘env` - An instance of a ShopifyCLI::Resources::EnvFile

#### Example

ShopifyCLI::Project.current.env


141
142
143
144
145
146
147
# File 'lib/shopify_cli/project.rb', line 141

def env
  @env ||= begin
             Resources::EnvFile.read(directory)
           rescue Errno::ENOENT
             nil
           end
end