# frozen_string_literal: true require "json" require "yaml" module Makit module V1 class Project def to_yaml data = JSON.parse(self.to_pretty_json) data.to_yaml.sub(/^---\n/, "") end def self.create project = Makit::V1::Project.new project.set_default_values project end def open(filename) other = Makit::V1::Project::create_from_file(filename) self.name = other.name self.git_remote_url = other.git_remote_url self.dotnet_projects = other.dotnet_projects end def self.create_from_file(filename) extension = File.extname(filename) case extension when ".yml" yaml = File.read(filename) create_from_yaml(yaml) when ".json" json = File.read(filename) create_from_json(json) else raise "unsupported file extension: #{extension}" end end def self.create_from_yaml(yaml) json = JSON.pretty_generate(YAML.load(yaml)) project = Makit::V1::Project.decode_json(json) project end def self.create_from_json(json) project = Makit::V1::Project.decode_json(json) project end def set_default_values #self.language = "csharp" #self.primary_artifact_type = "nuget" if self.git_remote_url.nil? || self.git_remote_url.empty? self.git_remote_url = `git remote get-url origin`.strip end if self.name.nil? || self.name.empty? if (!self.git_remote_url.nil? && !self.git_remote_url.empty?) self.name = get_name(File.basename(self.git_remote_url, ".git")) # get_capitalized_name(File.basename(self.git_remote_url, ".git")) else self.name = get_name(File.basename(Dir.getwd)) # get_capitalized_name(File.basename(Dir.getwd)) end end self end def get_name(name) if !self.git_remote_url.nil? && !self.git_remote_url.empty? is_dotnet = self.git_remote_url.include?("nuget") if is_dotnet get_capitalized_name(name) end end name.downcase end def get_capitalized_name(name) name.split(".").map(&:capitalize).join(".") end def save_as(filename) extension = File.extname(filename) case extension when ".json" File.write(filename, self.to_pretty_json) when ".yml" File.write(filename, self.to_yaml) else raise "unsupported file extension: #{extension}" end end def with_dotnet_project(template, name, output) if !self.dotnet_projects.any? { |project| project.output == output } project = Makit::V1::DotNetProject.new project.template = template project.name = name project.output = output self.dotnet_projects << project end end def setup setup_dotnet_projects end def build build_dotnet_projects end def setup_dotnet_projects if self.dotnet_projects.any? if (!File.exist?("#{self.name}.sln")) puts " Creating solution file: " + "#{self.name}.sln".colorize(:green) "dotnet new sln -n #{self.name}".run unless File.exist?("#{self.name}.sln") else #puts " Solution file already exists: " + "#{self.name}.sln".colorize(:yellow) end self.dotnet_projects.each do |project| add_project = true if (project.os == "windows" && !Makit::Environment.is_windows?) add_project = false end if (add_project) Makit::DotNet.new_project(project.template, project.name, project.output) # add any nuget packages to the project project_filename = "#{project.output}/#{project.name}.csproj" project.packages.each { |package| Makit::DotNet::add_package(project_filename, package) } end end Makit::DotNet.new_solution(self.name) Makit::DotNet.sln_add_projects(self.name) else puts " no dotnet projects found".colorize(:yellow) end end def build_dotnet_projects self.dotnet_projects.each do |project| if (project.os == "windows" && !Makit::Environment.is_windows?) next else project.build_args.each do |build_arg| project_path = File.join(project.output, "#{project.name}.csproj") Makit::DotNet.build(project_path, build_arg.configuration, build_arg.output) end project.publish_args.each do |publish_arg| project_path = File.join(project.output, "#{project.name}.csproj") Makit::DotNet.publish(project_path, publish_arg.configuration, publish_arg.output) end end end end def test_dotnet_projects self.dotnet_projects.each do |project| "dotnet test #{project.output} --configuration Release".run end end end # class Project end # module V1 end # module Makit