Class: Makit::DotNet::Project
- Inherits:
-
Object
- Object
- Makit::DotNet::Project
- Defined in:
- lib/makit/dotnet/project.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
Class Method Summary collapse
- .add_package(project_path, package_name) ⇒ Object
- .add_packages(project_path, packages) ⇒ Object
- .add_reference(project_path, reference_path) ⇒ Object
- .add_references(project_path, references) ⇒ Object
- .build(project_path, configuration = "Release", output = "artifacts") ⇒ Object
- .find_project(project_hint) ⇒ Object
- .format_project(project_path) ⇒ Object
-
.installed? ⇒ Boolean
consider move some of the methods to the CLI class.
- .new_project(template, name, output, args = "") ⇒ Object
- .project_short_name(project_path) ⇒ Object
- .publish(project_path, configuration = "Release", output = "artifacts") ⇒ Object
- .set_project_target_framework(project_hint, target_framework) ⇒ Object
- .test(project_path, configuration = "Release", output = "artifacts") ⇒ Object
- .upgrade_project(project_path) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #get_version ⇒ Object
-
#initialize(filename) ⇒ Project
constructor
A new instance of Project.
- #set_nuget_metadata(package_id, authors, description, license_expression) ⇒ Object
-
#set_target_frameworks(target_frameworks) ⇒ Object
<TargetFramework>net8.0</TargetFramework> <TargetFrameworks>net8.0;net8.0-browser</TargetFrameworks>.
- #set_version(version) ⇒ Object
Constructor Details
#initialize(filename) ⇒ Project
Returns a new instance of Project.
8 9 10 |
# File 'lib/makit/dotnet/project.rb', line 8 def initialize(filename) @filename = filename end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
6 7 8 |
# File 'lib/makit/dotnet/project.rb', line 6 def filename @filename end |
Class Method Details
.add_package(project_path, package_name) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/makit/dotnet/project.rb', line 117 def self.add_package(project_path, package_name) unless File.exist?(project_path) actual_project_path = Makit::DotNet::Project.find_project(project_path) raise "Project #{project_path} does not exist".colorize(:red) unless File.exist?(actual_project_path) project_path = actual_project_path end project_content = File.read(project_path) if project_content.include?("\"#{package_name}\"") puts " package ".colorize(:grey) + package_name.to_s.colorize(:green) else "dotnet add #{project_path} package #{package_name}".run end end |
.add_packages(project_path, packages) ⇒ Object
133 134 135 136 137 |
# File 'lib/makit/dotnet/project.rb', line 133 def self.add_packages(project_path, packages) packages.each do |package| add_package(project_path, package) end end |
.add_reference(project_path, reference_path) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/makit/dotnet/project.rb', line 139 def self.add_reference(project_path, reference_path) unless File.exist?(project_path) actual_project_path = Makit::DotNet::Project.find_project(project_path) raise "Project #{project_path} does not exist".colorize(:red) unless File.exist?(actual_project_path) project_path = actual_project_path end unless File.exist?(reference_path) actual_reference_path = Makit::DotNet::Project.find_project(reference_path) raise "Project #{reference_path} does not exist".colorize(:red) unless File.exist?(actual_reference_path) reference_path = actual_reference_path end project_content = File.read(project_path) if project_content.include?(File.basename(reference_path)) # puts " reference ".colorize(:grey) + reference_path.to_s.colorize(:green) else "dotnet add #{project_path} reference #{reference_path}".run end end |
.add_references(project_path, references) ⇒ Object
162 163 164 165 166 |
# File 'lib/makit/dotnet/project.rb', line 162 def self.add_references(project_path, references) references.each do |reference| add_reference(project_path, reference) end end |
.build(project_path, configuration = "Release", output = "artifacts") ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/makit/dotnet/project.rb', line 168 def self.build(project_path, configuration = "Release", output = "artifacts") project_dir = File.dirname(project_path) newest_file = Makit::Directory.get_newest_file(project_dir) command_request = Makit::Commands::Runner.default.parse_command_request("dotnet build #{project_path} --configuration #{configuration} --output #{output}") newest_file_date = if newest_file.nil? Time.now else File.mtime(newest_file) end RUNNER.cache_run(command_request, newest_file_date) end |
.find_project(project_hint) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/makit/dotnet/project.rb', line 103 def self.find_project(project_hint) matches = [] Dir.glob("**/*.csproj").each do |project_path| matches << project_path if project_path.include?(project_hint) end if matches.length == 1 return matches.first elsif matches.length > 1 raise "Multiple projects found matching #{project_hint}".colorize(:red) end raise "No project found matching #{project_hint}".colorize(:red) end |
.format_project(project_path) ⇒ Object
204 205 206 207 208 |
# File 'lib/makit/dotnet/project.rb', line 204 def self.format_project(project_path) File.basename(project_path, ".csproj") "dotnet tool update --global dotnet-format".run "dotnet format #{project_path}".run end |
.installed? ⇒ Boolean
consider move some of the methods to the CLI class
74 75 76 |
# File 'lib/makit/dotnet/project.rb', line 74 def self.installed? is_installed? end |
.new_project(template, name, output, args = "") ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/makit/dotnet/project.rb', line 86 def self.new_project(template, name, output, args = "") if Dir.exist? output Makit::Logging.default_logger.info("#{output}/#{name}.csproj") # puts " .csproj ".colorize(:grey) + name.to_s.colorize(:green) else "dotnet new #{template} --name #{name} --output #{output} #{args}".run end end |
.project_short_name(project_path) ⇒ Object
82 83 84 |
# File 'lib/makit/dotnet/project.rb', line 82 def self.project_short_name(project_path) File.basename(project_path, ".csproj") end |
.publish(project_path, configuration = "Release", output = "artifacts") ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/makit/dotnet/project.rb', line 180 def self.publish(project_path, configuration = "Release", output = "artifacts") project_dir = File.dirname(project_path) newest_file = Makit::Directory.get_newest_file(project_dir) command_request = Makit::Commands::Runner.default.parse_command_request("dotnet publish #{project_path} --configuration #{configuration} --output #{output}") newest_file_date = if newest_file.nil? Time.now else File.mtime(newest_file) end RUNNER.cache_run(command_request, newest_file_date) end |
.set_project_target_framework(project_hint, target_framework) ⇒ Object
95 96 97 98 99 100 101 |
# File 'lib/makit/dotnet/project.rb', line 95 def self.set_project_target_framework(project_hint, target_framework) project_path = find_project(project_hint) project_content = File.read(project_path) project_content.gsub!(%r{<TargetFramework>.*</TargetFramework>}, "<TargetFramework>#{target_framework}</TargetFramework>") File.write(project_path, project_content) end |
.test(project_path, configuration = "Release", output = "artifacts") ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/makit/dotnet/project.rb', line 192 def self.test(project_path, configuration = "Release", output = "artifacts") project_dir = File.dirname(project_path) newest_file = Makit::Directory.get_newest_file(project_dir) command_request = Makit::Commands::Runner.default.parse_command_request("dotnet test #{project_path} --configuration #{configuration} --output #{output}") newest_file_date = if newest_file.nil? Time.now else File.mtime(newest_file) end RUNNER.cache_run(command_request, newest_file_date) end |
.upgrade_project(project_path) ⇒ Object
210 211 212 213 214 |
# File 'lib/makit/dotnet/project.rb', line 210 def self.upgrade_project(project_path) File.basename(project_path, ".csproj") "dotnet tool install --global dotnet-outdated-tool".run "dotnet outdated #{project_path} --upgrade:Auto".run end |
.version ⇒ Object
78 79 80 |
# File 'lib/makit/dotnet/project.rb', line 78 def self.version `dotnet --version` end |
Instance Method Details
#get_version ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/makit/dotnet/project.rb', line 26 def get_version # get the <Version> tag project_content = File.read(filename) raise "Version not found in #{filename}".colorize(:red) unless project_content.include?("<Version>") project_content.match(%r{<Version>(.*)</Version>})[1] end |
#set_nuget_metadata(package_id, authors, description, license_expression) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/makit/dotnet/project.rb', line 59 def (package_id, , description, license_expression) project_content = File.read(filename) if project_content.include?("<PropertyGroup Label=\"NuGet Metadata\">") project_content.gsub!(%r{<PropertyGroup Label="NuGet Metadata">.*</PropertyGroup>}, " <PropertyGroup Label=\"NuGet Metadata\">\n <IsPackable>true</IsPackable>\n <PackageId>#{package_id}</PackageId>\n <Authors>#{}</Authors>\n <Description>#{description}</Description>\n <PackageLicenseExpression>#{license_expression}</PackageLicenseExpression>\n\n</PropertyGroup>") else # add the <PropertyGroup Label=\"NuGet Metadata\"> tag before the </Project> closing tag project_content.gsub!(%r{</Project>}, " <PropertyGroup Label=\"NuGet Metadata\">\n <IsPackable>true</IsPackable>\n <PackageId>#{package_id}</PackageId>\n <Authors>#{}</Authors>\n <Description>#{description}</Description>\n <PackageLicenseExpression>#{license_expression}</PackageLicenseExpression>\n </PropertyGroup>\n\n</Project>") end File.write(filename, project_content) end |
#set_target_frameworks(target_frameworks) ⇒ Object
<TargetFramework>net8.0</TargetFramework> <TargetFrameworks>net8.0;net8.0-browser</TargetFrameworks>
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/makit/dotnet/project.rb', line 36 def set_target_frameworks(target_frameworks) project_content = File.read(filename) target_frameworks = target_frameworks.join(";") if target_frameworks.is_a?(Array) if target_frameworks.include?(";") # use <TargetFrameworks> tag if project_content.include?("<TargetFrameworks>") project_content.gsub!(%r{<TargetFrameworks>.*</TargetFrameworks>}, "<TargetFrameworks>#{target_frameworks}</TargetFrameworks>") else project_content.gsub!(%r{<TargetFramework>.*</TargetFramework>}, "<TargetFrameworks>#{target_frameworks}</TargetFrameworks>") end elsif project_content.include?("<TargetFramework>") # use <TargetFramework> tag project_content.gsub!(%r{<TargetFramework>.*</TargetFramework>}, "<TargetFramework>#{target_frameworks}</TargetFramework>") else project_content.gsub!(%r{<TargetFrameworks>.*</TargetFrameworks>}, "<TargetFramework>#{target_frameworks}</TargetFramework>") end File.write(filename, project_content) end |
#set_version(version) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/makit/dotnet/project.rb', line 12 def set_version(version) # set the <Version> tag to the version project_content = File.read(filename) if project_content.include?("<Version>") project_content.gsub!(%r{<Version>.*</Version>}, "<Version>#{version}</Version>") else # add the <Version> tag to the next line after the <PropertyGroup> tag project_content.gsub!("<PropertyGroup>", "<PropertyGroup>\n <Version>#{version}</Version>") end File.write(filename, project_content) end |