lib/makit/directories.rb in makit-0.0.1 vs lib/makit/directories.rb in makit-0.0.2

- old
+ new

@@ -1,140 +1,140 @@ -# frozen_string_literal: true - -require_relative "directory" -require_relative "environment" -require_relative "protoc" -require_relative "nuget" -require_relative "version" -require "rake" - -# This module provides classes for the Makit gem. -module Makit - # This class provide methods for working with the system Environment. - # - module Directories - ROOT = File.join(Dir.home, ".makit") - CLONE = File.join(Dir.home, ".makit", "clone") - MAKE = File.join(Dir.home, ".makit", "make") - LOG = File.join(Dir.home, ".makit", "log") - WORK = File.join(Dir.home, ".makit", "work") - REPOS = File.join(Dir.home, ".makit", "repos") - TEMP = File.join(Dir.home, ".makit", "temp") - INSTALL = File.join(Dir.home, ".makit", "install") - HOME = Makit::Directory.normalize(Dir.home) - ONEDRIVE = File.join(Dir.home, "OneDrive") - CURRENT = Dir.pwd - #Rake.application.init - #Rake.application.load_rakefile - RAKEDIR = Dir.pwd #File.dirname(Rake.application.rakefile) - - PROJECT_ROOT = Makit::Directory.find_directory_with_patterns(RAKEDIR, ["Rakefile", "rakefile.rb", ".gitignore"]) - #PROJECT_ROOT = Makit::Directory.find_directory_with_pattern(RAKEDIR, "Rakefile") - #PROJECT_ROOT = Makit::Directory.find_directory_with_pattern(RAKEDIR, "rakefile.rb") - #PROJECT_ROOT = Makit::Directory.find_directory_with_pattern(RAKEDIR, ".gitignore") - if (PROJECT_ROOT.nil?) - PROJECT_ARTIFACTS = nil - else - PROJECT_ARTIFACTS = File.join(PROJECT_ROOT, "artifacts") - end - LOCAL_NUGET_SOURCE = File.join(Dir.home, ".makit", "nuget") - if Makit::Environment::get_os == "Windows" - #NUGET_PACKAGE_CACHE = "#{ENV["USERPROFILE"]}\\.nuget\\packages" - NUGET_PACKAGE_CACHE = File.join(HOME, ".nuget", "packages") - SERVICES = File.join("C:", "Program Files") - else - NUGET_PACKAGE_CACHE = Makit::Directory.normalize("#{ENV["HOME"]}/.nuget/packages") - if Makit::Environment::get_os == "macOS" - SERVICES = "/Library/LaunchDaemons" - else - SERVICES = "/etc/systemd/system" - end - end - if Makit::NuGet::get_latest_version("Grpc.Tools").nil? - puts " warning: Grpc.Tools not found in Nuget cache" - else - GRPC_TOOLS_PATH = File.join(NUGET_PACKAGE_CACHE, "grpc.tools", Makit::Protoc::get_latest_grpc_tools_version) - GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path("grpc_csharp_plugin") - end - - Dir.mkdir(ROOT) unless Dir.exist?(ROOT) - Dir.mkdir(CLONE) unless Dir.exist?(CLONE) - Dir.mkdir(MAKE) unless Dir.exist?(MAKE) - Dir.mkdir(LOG) unless Dir.exist?(LOG) - Dir.mkdir(WORK) unless Dir.exist?(WORK) - Dir.mkdir(REPOS) unless Dir.exist?(REPOS) - Dir.mkdir(TEMP) unless Dir.exist?(TEMP) - Dir.mkdir(INSTALL) unless Dir.exist?(INSTALL) - if (!Dir.exist?(LOCAL_NUGET_SOURCE)) - Dir.mkdir(LOCAL_NUGET_SOURCE) - system("dotnet nuget add source #{LOCAL_NUGET_SOURCE} --name local") - end - - def self.get_project_path(relative_path) - File.join(PROJECT_ROOT, relative_path) - end - - def self.get_clone_directory(url) - File.join(CLONE, get_relative_directory(url)) - end - - def self.get_work_directory(url) - File.join(WORK, get_relative_directory(url)) - end - - def self.get_make_directory(url) - File.join(MAKE, get_relative_directory(url)) - end - - def self.get_make_commit_directory(url, commit_id) - File.join(MAKE, get_relative_directory(url), commit_id) - end - - def self.get_log_directory(url) - File.join(LOG, get_relative_directory(url)) - end - def self.get_make_commit_log_filename(url, commit_id) - File.join(LOG, get_relative_directory(url), commit_id + ".json") - end - - def self.get_relative_directory(url) - # if url start with Directories::REPOS, then it is a local repository - if url.start_with?(REPOS) - return url.gsub(REPOS, "") - end - url = url.gsub("https://", "").gsub("http://", "") - url = url.gsub("gitlab.com", "gitlab") - url = url.gsub("github.com", "github") - - # if the url ends with .git, remove it - url = url.gsub(".git", "") if url.end_with?(".git") - url - end - - # show all the directory constants in a nicely formatted table format with the name of the constant and the value - def self.show - # Array of constant names (symbols) - constant_names = [:ROOT, :CLONE, :MAKE, :LOG, :WORK, :REPOS, :TEMP, :INSTALL, :HOME, :ONEDRIVE, - :CURRENT, :RAKEDIR, - :PROJECT_ROOT, :PROJECT_ARTIFACTS, :LOCAL_NUGET_SOURCE, :NUGET_PACKAGE_CACHE, :SERVICES, - :GRPC_TOOLS_PATH, :GRPC_CSHARP_PLUGIN_PATH] - - # Find the length of the longest constant name and add 1 - max_length = constant_names.map(&:to_s).max_by(&:length).length + 1 - - # Iterate through each constant name - constant_names.each do |constant_name| - begin - constant_value = const_get(constant_name) # Fetch the value of the constant - if (constant_value != nil && Dir.exist?(constant_value)) - constant_value = constant_value.colorize(:green) - end - # Print the constant name right justified to the max_length - puts "#{constant_name.to_s.rjust(max_length)} = #{constant_value}" - rescue NameError - # Handle the case where the constant is not defined - puts "#{constant_name.to_s.rjust(max_length)} = [Constant not defined]" - end - end - end - end # end module Directories -end # end module Makit +# frozen_string_literal: true + +require_relative "directory" +require_relative "environment" +require_relative "protoc" +require_relative "nuget" +require_relative "version" +require "rake" + +# This module provides classes for the Makit gem. +module Makit + # This class provide methods for working with the system Environment. + # + module Directories + ROOT = File.join(Dir.home, ".makit") + CLONE = File.join(Dir.home, ".makit", "clone") + MAKE = File.join(Dir.home, ".makit", "make") + LOG = File.join(Dir.home, ".makit", "log") + WORK = File.join(Dir.home, ".makit", "work") + REPOS = File.join(Dir.home, ".makit", "repos") + TEMP = File.join(Dir.home, ".makit", "temp") + INSTALL = File.join(Dir.home, ".makit", "install") + HOME = Makit::Directory.normalize(Dir.home) + ONEDRIVE = File.join(Dir.home, "OneDrive") + CURRENT = Dir.pwd + #Rake.application.init + #Rake.application.load_rakefile + RAKEDIR = Dir.pwd #File.dirname(Rake.application.rakefile) + + PROJECT_ROOT = Makit::Directory.find_directory_with_patterns(RAKEDIR, ["Rakefile", "rakefile.rb", ".gitignore"]) + #PROJECT_ROOT = Makit::Directory.find_directory_with_pattern(RAKEDIR, "Rakefile") + #PROJECT_ROOT = Makit::Directory.find_directory_with_pattern(RAKEDIR, "rakefile.rb") + #PROJECT_ROOT = Makit::Directory.find_directory_with_pattern(RAKEDIR, ".gitignore") + if (PROJECT_ROOT.nil?) + PROJECT_ARTIFACTS = nil + else + PROJECT_ARTIFACTS = File.join(PROJECT_ROOT, "artifacts") + end + LOCAL_NUGET_SOURCE = File.join(Dir.home, ".makit", "nuget") + if Makit::Environment::get_os == "Windows" + #NUGET_PACKAGE_CACHE = "#{ENV["USERPROFILE"]}\\.nuget\\packages" + NUGET_PACKAGE_CACHE = File.join(HOME, ".nuget", "packages") + SERVICES = File.join("C:", "Program Files") + else + NUGET_PACKAGE_CACHE = Makit::Directory.normalize("#{ENV["HOME"]}/.nuget/packages") + if Makit::Environment::get_os == "macOS" + SERVICES = "/Library/LaunchDaemons" + else + SERVICES = "/etc/systemd/system" + end + end + if Makit::NuGet::get_latest_version("Grpc.Tools").nil? + puts " warning: Grpc.Tools not found in Nuget cache" + else + GRPC_TOOLS_PATH = File.join(NUGET_PACKAGE_CACHE, "grpc.tools", Makit::Protoc::get_latest_grpc_tools_version) + GRPC_CSHARP_PLUGIN_PATH = Makit::Protoc::find_plugin_path("grpc_csharp_plugin") + end + + Dir.mkdir(ROOT) unless Dir.exist?(ROOT) + Dir.mkdir(CLONE) unless Dir.exist?(CLONE) + Dir.mkdir(MAKE) unless Dir.exist?(MAKE) + Dir.mkdir(LOG) unless Dir.exist?(LOG) + Dir.mkdir(WORK) unless Dir.exist?(WORK) + Dir.mkdir(REPOS) unless Dir.exist?(REPOS) + Dir.mkdir(TEMP) unless Dir.exist?(TEMP) + Dir.mkdir(INSTALL) unless Dir.exist?(INSTALL) + if (!Dir.exist?(LOCAL_NUGET_SOURCE)) + Dir.mkdir(LOCAL_NUGET_SOURCE) + system("dotnet nuget add source #{LOCAL_NUGET_SOURCE} --name local") + end + + def self.get_project_path(relative_path) + File.join(PROJECT_ROOT, relative_path) + end + + def self.get_clone_directory(url) + File.join(CLONE, get_relative_directory(url)) + end + + def self.get_work_directory(url) + File.join(WORK, get_relative_directory(url)) + end + + def self.get_make_directory(url) + File.join(MAKE, get_relative_directory(url)) + end + + def self.get_make_commit_directory(url, commit_id) + File.join(MAKE, get_relative_directory(url), commit_id) + end + + def self.get_log_directory(url) + File.join(LOG, get_relative_directory(url)) + end + def self.get_make_commit_log_filename(url, commit_id) + File.join(LOG, get_relative_directory(url), commit_id + ".json") + end + + def self.get_relative_directory(url) + # if url start with Directories::REPOS, then it is a local repository + if url.start_with?(REPOS) + return url.gsub(REPOS, "") + end + url = url.gsub("https://", "").gsub("http://", "") + url = url.gsub("gitlab.com", "gitlab") + url = url.gsub("github.com", "github") + + # if the url ends with .git, remove it + url = url.gsub(".git", "") if url.end_with?(".git") + url + end + + # show all the directory constants in a nicely formatted table format with the name of the constant and the value + def self.show + # Array of constant names (symbols) + constant_names = [:ROOT, :CLONE, :MAKE, :LOG, :WORK, :REPOS, :TEMP, :INSTALL, :HOME, :ONEDRIVE, + :CURRENT, :RAKEDIR, + :PROJECT_ROOT, :PROJECT_ARTIFACTS, :LOCAL_NUGET_SOURCE, :NUGET_PACKAGE_CACHE, :SERVICES, + :GRPC_TOOLS_PATH, :GRPC_CSHARP_PLUGIN_PATH] + + # Find the length of the longest constant name and add 1 + max_length = constant_names.map(&:to_s).max_by(&:length).length + 1 + + # Iterate through each constant name + constant_names.each do |constant_name| + begin + constant_value = const_get(constant_name) # Fetch the value of the constant + if (constant_value != nil && Dir.exist?(constant_value)) + constant_value = constant_value.colorize(:green) + end + # Print the constant name right justified to the max_length + puts "#{constant_name.to_s.rjust(max_length)} = #{constant_value}" + rescue NameError + # Handle the case where the constant is not defined + puts "#{constant_name.to_s.rjust(max_length)} = [Constant not defined]" + end + end + end + end # end module Directories +end # end module Makit