lib/skippy/project.rb in skippy-0.1.1.a vs lib/skippy/project.rb in skippy-0.2.0.a

- old
+ new

@@ -1,30 +1,57 @@ require 'json' require 'pathname' +require 'skippy/helpers/file' +require 'skippy/config' +require 'skippy/config_accessors' +require 'skippy/error' +require 'skippy/library_manager' require 'skippy/namespace' +require 'skippy/module_manager' class Skippy::Project + extend Skippy::ConfigAccessors + + include Skippy::Helpers::File + PROJECT_FILENAME = 'skippy.json'.freeze - attr_reader :name, :namespace, :path, :author, :copyright, :license - attr_accessor :description + attr_reader :config + attr_reader :libraries + attr_reader :modules + config_attr :author, :copyright, :description, :license, :name + config_attr :namespace, type: Skippy::Namespace + + class ProjectNotFoundError < Skippy::Error; end + class ProjectNotSavedError < Skippy::Error; end + + # @return [Skippy::Project, nil] + def self.current + Skippy::Project.new(Dir.pwd) + end + + # @return [Skippy::Project] + def self.current_or_fail + project = self.current + raise ProjectNotFoundError unless project.exist? + project + end + # Initialize a project for the provided path. If the path is within a project # path the base path of the project will be found. Otherwise it's assumed that # the path is the base for a new project. # # @param [Pathname, String] path def initialize(path) @path = find_project_path(path) || Pathname.new(path) - @namespace = Skippy::Namespace.new('Untitled') - @name = '' - @description = '' - @author = 'Unknown' - @copyright = "Copyright (c) #{Time.now.year}" - @license = 'None' + # noinspection RubyResolve + @config = Skippy::Config.load(filename, defaults) + @libraries = Skippy::LibraryManager.new(self) + @modules = Skippy::ModuleManager.new(self) end # @yield [filename] # @yieldparam [String] filename the path to custom Skippy command def command_files(&block) @@ -34,44 +61,46 @@ } end # Checks if a project exist on disk. If not it's just transient. def exist? - File.exist?(filename) + filename.exist? end # Full path to the project's configuration file. This file may not exist. - # @return [String] + # @return [Pathname] def filename - File.join(path, PROJECT_FILENAME) + path.join(PROJECT_FILENAME) end - # @return [String] - def name - @name.empty? ? namespace.to_name : @name + # @param [Pathname, String] sub_path + # @return [Pathname] + def path(sub_path = '') + @path.join(sub_path) end - # @param [String] namespace - def namespace=(namespace) - @namespace = Skippy::Namespace.new(namespace) - end - # Commits the project to disk. def save - File.write(filename, to_json) + @config.save_as(filename) end # @return [String] def to_json - project_config = { - namespace: namespace, - name: name, - description: description - } - JSON.pretty_generate(project_config) + JSON.pretty_generate(@config) end private + + def defaults + { + name: 'Untitled', + description: '', + namespace: Skippy::Namespace.new('Untitled'), + author: 'Unknown', + copyright: "Copyright (c) #{Time.now.year}", + license: 'None', + } + end # Finds the root of a project based on any path within the project. # # @param [String] path # return [Pathname, nil]