Class: Makit::NuGet

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/nuget.rb

Overview

This class provide methods for working with the Nuget package cache

Example:

Makit::Directory.cache("Google.Protobuf", "3.27.2")

dotnet nuget locals all –list dotnet nuget locals all –clear

Class Method Summary collapse

Class Method Details

.add_package(_package, path) ⇒ Object

publish a package to a nuget directory feed



61
62
63
# File 'lib/makit/nuget.rb', line 61

def self.add_package(_package, path)
  system("dotnet nuget push #{path} --source #{path}")
end

.cache(package, version) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/makit/nuget.rb', line 20

def self.cache(package, version)
  # if the package is already cached, there is nothing to do

  return if Dir.exist?(get_cache_dir(package, version))

  Dir.mktmpdir do |dir|
    # Use the temp directory here

    Dir.chdir(dir) do
      system("dotnet new classlib -n ClassLib")
      Dir.chdir("ClassLib") do
        # display a list of directories in the current directory

        puts Dir.entries(Dir.pwd)
        # display a list of files in the current directory

        puts Dir.entries(Dir.pwd)
        puts "dotnet add ClassLib.csproj package #{package} --version #{version}"
        system("dotnet add ClassLib.csproj package #{package} --version #{version}")
      end
    end
    # The directory and its contents will be removed automatically after the block

  end
end

.clear_cache(package, version) ⇒ Object



41
42
43
44
45
# File 'lib/makit/nuget.rb', line 41

def self.clear_cache(package, version)
  return unless Dir.exist?(get_cache_dir(package, version))

  FileUtils.rm_rf(get_cache_dir(package, version))
end

.get_cache_dir(package, version) ⇒ Object



16
17
18
# File 'lib/makit/nuget.rb', line 16

def self.get_cache_dir(package, version)
  File.join(Makit::Directories::NUGET_PACKAGE_CACHE, package, version)
end

.get_latest_version(package) ⇒ Object

get the latest version of the package



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/makit/nuget.rb', line 48

def self.get_latest_version(package)
  dir = File.join(Makit::Directories::NUGET_PACKAGE_CACHE, package)
  if Dir.exist?(dir)
    versions = Dir.entries(dir).select do |entry|
      File.directory?(File.join(dir, entry)) && ![".", ".."].include?(entry)
    end
    highest_version = Makit::Version.get_highest_version(versions)
    return highest_version
  end
  nil
end

.publish_to_directory(nuget_package_path, directory, package_name, version) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/makit/nuget.rb', line 65

def self.publish_to_directory(nuget_package_path, directory, package_name, version)
  target_package_path = "#{directory}/#{package_name}/#{version}/#{package_name}.#{version}.nupkg".downcase
  if File.exist?(target_package_path)
    puts "  #{target_package_path} already exists".colorize(:grey)
  else
    "dotnet nuget push #{nuget_package_path} --source #{directory}".run
  end
end