Sha256: bcaaa67c7fdadbd89da90645b2f9cdffdb0d4ddda5d97dc138306c0f2c9e9f01

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

require "carggo/version"
require "thor"
require 'net/http'
require 'json'
require 'fileutils'

module Carggo
  class Error < StandardError; end

  class CLI < Thor
    desc "find", "generally useles, since Cargo provides this functionality"
    def find(args)
      begin
        escape_uri = URI.escape("https://crates.io/api/v1/crates//#{args}")
        uri = URI.parse(escape_uri)
        res = JSON.parse(Net::HTTP.get(uri))
        name = res["crate"]["name"]
        version = res["crate"]["max_version"]
        printable_response = "#{name} = '#{version}'"
        puts printable_response
        return {name: name, version: version}
      rescue
        puts "Crate not found"
      end
    end

    desc "add", "adds a crate(crates) to your Cargo.toml dependencies"
    def add(*args)
      if File.exists?("Cargo.toml")
        args.each do |lib|
          crate = self.find(lib)
          name = crate[:name]
          version = crate[:version]
          File.open("Cargo.toml", "r+") do |out|
            File.foreach("Cargo.toml") do |line|
              if line =~ /#{name}/
                next
              end
              out << line
              if line =~ /ependen/
                out << "\n#{name} = '#{version}'"
              end
            end
          end
        end
      else
        puts "Not a Cargo project directory."
      end
    end

    desc "remove", "removes the specified dependency"
    def remove(*args)
      if File.exists?("Cargo.toml")
        args.each do |lib|
          open("Cargo.toml", 'r') do |out|
            open("Cargo.toml.tmp", 'w') do |out2|
              out.each_line do |line|
                out2.write(line) unless line.start_with? lib
              end
            end
          end
        end
        FileUtils.mv 'Cargo.toml.tmp', 'Cargo.toml'
      else
        puts "Not a Cargo project directory."
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carggo-0.1.0 lib/carggo.rb