Initial import

This commit is contained in:
Dan Milne
2025-01-19 10:42:59 +11:00
commit 05d55ca665
20 changed files with 439 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@@ -0,0 +1,8 @@
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/

8
.rubocop.yml Normal file
View File

@@ -0,0 +1,8 @@
AllCops:
TargetRubyVersion: 3.1
Style/StringLiterals:
EnforcedStyle: double_quotes
Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes

5
CHANGELOG.md Normal file
View File

@@ -0,0 +1,5 @@
## [Unreleased]
## [0.1.0] - 2025-01-19
- Initial release

12
Gemfile Normal file
View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
source "https://rubygems.org"
# Specify your gem's dependencies in picop.gemspec
gemspec
gem "rake", "~> 13.0"
gem "minitest", "~> 5.16"
gem "rubocop", "~> 1.21"

49
Gemfile.lock Normal file
View File

@@ -0,0 +1,49 @@
PATH
remote: .
specs:
picop (0.1.0)
GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
json (2.9.1)
language_server-protocol (3.17.0.3)
minitest (5.25.4)
parallel (1.26.3)
parser (3.3.6.0)
ast (~> 2.4.1)
racc
racc (1.8.1)
rainbow (3.1.1)
rake (13.2.1)
regexp_parser (2.10.0)
rubocop (1.70.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.36.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.37.0)
parser (>= 3.3.1.0)
ruby-progressbar (1.13.0)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)
PLATFORMS
arm64-darwin-24
ruby
DEPENDENCIES
minitest (~> 5.16)
picop!
rake (~> 13.0)
rubocop (~> 1.21)
BUNDLED WITH
2.6.2

21
LICENSE.txt Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2025 Dan Milne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

39
README.md Normal file
View File

@@ -0,0 +1,39 @@
# Picop
TODO: Delete this and the text below, and describe your gem
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/picop`. To experiment with that code, run `bin/console` for an interactive prompt.
## Installation
TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
Install the gem and add to the application's Gemfile by executing:
```bash
bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
```
If bundler is not being used to manage dependencies, install the gem by executing:
```bash
gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
```
## Usage
TODO: Write usage instructions here
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/picop.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

12
Rakefile Normal file
View File

@@ -0,0 +1,12 @@
# frozen_string_literal: true
require "bundler/gem_tasks"
require "minitest/test_task"
Minitest::TestTask.create
require "rubocop/rake_task"
RuboCop::RakeTask.new
task default: %i[test rubocop]

11
bin/console Executable file
View File

@@ -0,0 +1,11 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require "bundler/setup"
require "picop"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
require "irb"
IRB.start(__FILE__)

8
bin/setup Executable file
View File

@@ -0,0 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here

15
exe/picop Executable file
View File

@@ -0,0 +1,15 @@
#!/usr/bin/env ruby
# Add lib directory to load path
lib_path = File.expand_path('../lib', __dir__)
$LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
require 'picop'
begin
Geckin::CLI.run(ARGV)
rescue => e
warn "Error: #{e.message}"
warn e.backtrace if ENV['DEBUG']
exit 1
end

11
lib/picop.rb Normal file
View File

@@ -0,0 +1,11 @@
# frozen_string_literal: true
require_relative "picop/version"
require_relative "picop/source_file"
require_relative "picop/scanner"
require_relative "picop/cli"
module Picop
class Error < StandardError; end
# Your code goes here...
end

88
lib/picop/cli.rb Normal file
View File

@@ -0,0 +1,88 @@
require "optparse"
module Picop
class CLI
def self.run(argv = ARGV)
command = argv.shift
case command
when 'scan'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: picop scan [options] DIRECTORY"
opts.on('-v', '--verbose', 'Run verbosely') { |v| options[:verbose] = v }
end.parse!(argv)
dir = argv.first || '.'
Picop::Scanner.scan(dir, options)
when 'sign'
OptionParser.new do |opts|
opts.banner = "Usage: picop sign FILE"
end.parse!(argv)
file = argv.first
Picop::SourceFile.new(file).sign
when 'checksum'
OptionParser.new do |opts|
opts.banner = "Usage: picop checksum FILE"
end.parse!(argv)
file = argv.first
puts Picop::SourceFile.new(file).checksum
when 'verify'
OptionParser.new do |opts|
opts.banner = "Usage: picop sign FILE"
end.parse!(argv)
path = argv.first
source = SourceFile.new(path)
if source.metadata['content_checksum'].nil?
puts "⚠️ No checksum found in #{path}"
puts "Run 'picop sign #{path}' to add one"
exit 1
end
unless source.verify
puts "❌ Checksum verification failed for #{path}"
puts "Expected: #{source.metadata['content_checksum']}"
puts "Got: #{source.checksum}"
exit 1
end
puts "#{path} verified successfully"
when 'show'
OptionParser.new do |opts|
opts.banner = "Usage: picop show FILE|DIRECTORY"
end.parse!(argv)
path = argv.first
Picop.show(path)
when 'update'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: picop update [options] FILE"
opts.on('-f', '--force', 'Force update') { |f| options[:force] = f }
end.parse!(argv)
file = argv.first
Picop.update(file, options)
else
puts "Unknown command: #{command}"
puts "Available commands: scan, sign, show, update"
exit 1
end
rescue OptionParser::InvalidOption => e
puts e.message
exit 1
rescue => e
puts "Error: #{e.message}"
puts e.backtrace if ENV['DEBUG']
exit 1
end
end
end

11
lib/picop/scanner.rb Normal file
View File

@@ -0,0 +1,11 @@
module Picop
module Scanner
def self.scan(directory, pattern: "**/*")
Dir.glob(File.join(directory, pattern)).select do |file|
next unless File.file?(file)
content = File.read(file)
content.match?(SourceFile::METADATA_PATTERN)
end.map { |file| SourceFile.new(file) }
end
end
end

65
lib/picop/source_file.rb Normal file
View File

@@ -0,0 +1,65 @@
require "yaml"
require "digest"
module Picop
class SourceFile
attr_reader :file_path, :content
METADATA_PATTERN = /^\s*#\s*@META_START\n(.*?)^\s*#\s*@META_END/m
def initialize(file_path)
@file_path = file_path
@content = File.read(file_path)
end
def metadata
@metadata ||= begin
return {} unless content =~ METADATA_PATTERN
yaml_content = $1.lines.map do |line|
line.sub(/^\s*#\s?/, '').rstrip
end.join("\n")
YAML.safe_load(yaml_content)
end
end
def file_content = content.sub(/^\s*#\s*@META_START\n.*?^\s*#\s*@META_END\n*/m, '')
def checksum = "sha256:#{Digest::SHA256.hexdigest(file_content)}"
def show = puts(metadata.merge({checksum: checksum}))
def save = File.write(file_path, content)
def add_metadata(metadata_hash)
yaml_content = metadata_hash.to_yaml.strip
metadata_block = [
"# @META_START",
yaml_content.lines.map { |line| "# #{line}" }.join,
"# @META_END"
].join("\n")
if content =~ METADATA_PATTERN
@content = content.sub(METADATA_PATTERN, metadata_block)
else
@content = [metadata_block, content].join("\n\n")
end
end
def sign
hash = checksum
meta = metadata || {}
return puts "File already signed" if meta['content_checksum'] == hash
meta['content_checksum'] = "#{hash}"
add_metadata(meta)
save
end
def verify
return false unless metadata.key? 'content_checksum'
checksum == metadata['content_checksum']
end
end
end

5
lib/picop/version.rb Normal file
View File

@@ -0,0 +1,5 @@
# frozen_string_literal: true
module Picop
VERSION = "0.1.0"
end

43
picop.gemspec Normal file
View File

@@ -0,0 +1,43 @@
# frozen_string_literal: true
require_relative "lib/picop/version"
Gem::Specification.new do |spec|
spec.name = "picop"
spec.version = Picop::VERSION
spec.authors = ["Dan Milne"]
spec.email = ["d@nmilne.com"]
spec.summary = "TODO: Write a short summary, because RubyGems requires one."
spec.description = "TODO: Write a longer description or delete this line."
spec.homepage = "TODO: Put your gem's website or public repo URL here."
spec.license = "MIT"
spec.required_ruby_version = ">= 3.1.0"
spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
gemspec = File.basename(__FILE__)
spec.files = IO.popen(%w[git ls-files -z], chdir: __dir__, err: IO::NULL) do |ls|
ls.readlines("\x0", chomp: true).reject do |f|
(f == gemspec) ||
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
end
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_dependency "open-uri", "~> 0.5"
spec.add_dependency "yaml", "~> 0.4"
spec.add_dependency "digest"
spec.add_development_dependency "debug"
# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
end

View File

@@ -0,0 +1,9 @@
# @META_START
# home_url: https://github.com/user/repo/blob/main/path/to/file.rb
# source_url: https://github.com/user/repo/raw/#{version}/path/to/file.rb
# version: 1.0.0
# alsdfkasf
# content_hash: sha256:d747c448ce60a6ef1a8372f7c7eee10160075acbed6412a12c2f6d71ca8b2f76
# @META_END
require "cgi"
puts "hi"

6
test/test_helper.rb Normal file
View File

@@ -0,0 +1,6 @@
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "picop"
require "minitest/autorun"

13
test/test_picop.rb Normal file
View File

@@ -0,0 +1,13 @@
# frozen_string_literal: true
require "test_helper"
class TestPicop < Minitest::Test
def test_that_it_has_a_version_number
refute_nil ::Picop::VERSION
end
def test_it_does_something_useful
assert false
end
end