Project

cinnabar

0.0
No release in over 3 years
A handy toolkit tailored for CI workflows (e.g., GitHub Actions)
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Runtime

~> 0.0.8
 Project Readme

Cinnabar

Gem Version

A handy toolkit tailored for CI workflows.


Language/語言 ID
English en-Latn-US
简体中文 zh-Hans-CN
繁體中文 zh-Hant-TW

Preface

Q: Why is it named Cinnabar?

A:

  1. Cinnabar was an ingredient used by ancient Daoist alchemists in their elixirs, much like this project serves as an "ingredient" within CI workflows.
  2. Cinnabar is toxic. This project was developed for Dirty and Quick purposes and may produce unexpected side effects—in a sense, it is not entirely harmless.
  3. Cinnabar, a mineral form of mercury sulfide (HgS), is a deep red-colored stone. And ruby is also a deep red stone. Naming a Ruby project "Cinnabar" is particularly fitting.

Documentation

ClassDiagram

Quick Start

Github Actions for cinnabar

Image

Structure:

Your Project
│
├──.github
│     ├── _ci_
│     │    ├── preload_xx.rb
│     │    └── preload_yy.rb
│     └── workflows
│          └── build_zz.yml
├── cinnabar (Clone within the workflow)
│      ├── (...)
│      └── ci.rb (*)
└── (...)

workflow file:

env:
  # (Optional)
  # Relative to the position of **cinnabar/**
  CINNABAR_CI_DIR: '../.github/_ci_'

  # (Optional)
  # The value of CINNABAR_CI_LIBS is generally a list of files located in the directory CINNABAR_CI_DIR.
  # type: String: '["json", "array"]'
  CINNABAR_CI_LIBS: '["preload_xx.rb", "preload_yy.rb"]'

  # optional values: debug, info, warn, error, fatal, unknown
  RUBY_LOG: debug

defaults:
  run:
    # Speeds up script startup by disabling RubyGems
    shell: ruby --disable=gems cinnabar/ci.rb {0}

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6

      - name: clone cinnabar
        uses: actions/checkout@v6
        with:
          repository: 2moe/cinnabar
          path: cinnabar
          ref: v0.0.9-alpha.1

      - name: (example) run cargo command
        run: |
          {
            cargo: (),
            build: (),
            profile: 'release',
            verbose: true,
            target: 'x86_64-unknown-linux-musl'
          }
            .to_argv
            .run_cmd

Examples

Command Runner

.run + pass stdin data

- run: |
    opts = { stdin_data: "Hello", allow_failure: true }
    stdout = %w[wc -m].run(opts:)

    stdout.to_i == 5 #=> true

.async_run + log

- run: |
    'building wasi file...'.log_dbg
    task = {
      cargo: (),
      b: (),
      r: true,
      target: 'wasm32-wasip2'
    } .to_argv
      .async_run

    # log_dbg, log_info, log_warn, log_err, log_fatal, log_unk
    "You can now do other things without waiting for
    the process to complete.".log_dbg

    stdout, status = task.wait_with_output
    stdout.log_info
    raise "wasi" unless status.success?

.async_run + pass stdin data

- run: |
    stdin_data = <<~'QMP_JSON'
      { "execute":"qmp_capabilities" }
      { "execute":"query-cpu-model-expansion",
        "arguments":{"type":"full","model":{"name":"host"}} }
      { "execute":"quit" }
    QMP_JSON

    # opts = { stdin_data:, stdin_binmode: false }
    opts = { stdin_data: }

    accel = %w[kvm hvf whpx].join ':'
    task = {
      'qemu-system-x86_64': (),
      machine: "accel=#{accel}",
      cpu: 'host',
      display: 'none',
      nodefaults: true,
      no_user_config: true,
      qmp: 'stdio',
    } .to_argv_bsd
      .async_run(opts:)

    stdout, status = task.wait_with_output
    stdout.log_info if status.success?

Downloader

- run: |
    url = 'https://docs.ruby-lang.org/en/master'
    url.download
    # OR: url.download({out_dir: "/tmp", file_name: "index.html"})

Function Pipe

- run: |
    upper = ->s { s.upcase }

    'Foo'
      .(upper)
      . :puts #=> "FOO"

String To Pathname

- run: |
    __dir__.to_path #  Same as Pathname(__dir__)
      .join('tmp')

GemPath

GemPath is used mainly with Ruby started via the --disable=gems flag; the code below is equivalent to %w[ irb rdoc reline ].each { require it }.

def require_gems = ->(gems) {
  {
    # cache_dir: File.expand_path('~/.cache/ruby'),
    gems:,
  }
    .then(&Cinnabar.new_gem_path_proc)
    .append_load_path!

  gems.each { require _1 }
}

%w[
  irb rdoc reline
].then(&require_gems)

Faster IRB

Because GemPath automatically caches gem require_paths, combining it with ruby --disable=gems can significantly reduce IRB startup time.

  1. Run: misc/firb/install.ps1

OR:

gem install cinnabar
  • Enter the directory:
    • ruby -r cinnabar -e "p Cinnabar.firb_path"
    • OR: ${XDG_CACHE_HOME:-~/.cache}/ruby/firb/bin/ (POSIX sh)
    • OR: ~/.cache/ruby/firb/bin/
    • OR: %UserProfile%\.cache\ruby\firb\bin (Windows CMD)
  1. run