Skip to content

Commit

Permalink
feat: automatic updates and elint (#1)
Browse files Browse the repository at this point in the history
Adds an automatic updater to handle updating ebuilds in this repository
based on the latest version available on the remote.

Also puts `elint` in this repo since I've been working on this one more
than the former.
  • Loading branch information
jaredallard authored Feb 19, 2024
1 parent ba7a1bf commit c0d6f59
Show file tree
Hide file tree
Showing 39 changed files with 2,654 additions and 147 deletions.
19 changes: 7 additions & 12 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: jdx/mise-action@v2
with:
path: .src/overlay
# uses can't use working-directory, so we have to make this the
# default to use elint right now...
- uses: actions/checkout@v4
with:
repository: jaredallard/asahi-overlay
- uses: jdxcode/[email protected]
- name: Build elint
run: go build -o ./.bin/elint ./.elint/cmd/elint
- name: Run elint
working-directory: .src/overlay
run: ../../.bin/elint
experimental: true
- name: Build tools
working-directory: .tools
run: mise run build
- name: Run linter
run: mise run lint
34 changes: 34 additions & 0 deletions .github/workflows/update.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: "Check for updates"

on:
pull_request:
branches:
- "main"

schedule:
# Run every day at midnight, https://crontab.guru/#@daily.
- cron: "0 0 * * *"

jobs:
updater:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: jdx/mise-action@v2
with:
experimental: true
- name: Build tools
working-directory: .tools
run: mise run build
- name: Run updater
run: mise run update
- name: Run linter
run: mise run lint
- name: Create commit
if: github.event_name == 'schedule'
# TODO(jaredallard): The updater should handle this so we're
# more aware of what's being committed.
run: |-
git add -A .
git commit -am 'update ebuilds via updater'
git push
10 changes: 10 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[tools]
golang = "1.22"

[tasks.lint]
description = "Lint all ebuilds"
run = ["cd .tools; mise run build", ".tools/bin/elint"]

[tasks.update]
description = "Update all ebuilds"
run = ["cd .tools; mise run build", ".tools/bin/updater"]
1 change: 1 addition & 0 deletions .tools/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/
3 changes: 3 additions & 0 deletions .tools/.mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[tasks.build]
description = "Build all tools"
run = ["go build -trimpath -o bin/ ./cmd/..."]
23 changes: 23 additions & 0 deletions .tools/.vscode/common.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"Copyright": {
"description": "Inserts a GNU AGPLv3 license header",
"prefix": ["license", "copy", "copyright"],
"body": [
"// Copyright (C) ${CURRENT_YEAR} Jared Allard",
"//",
"// This program is free software: you can redistribute it and/or modify",
"// it under the terms of the GNU Affero General Public License as published by",
"// the Free Software Foundation, either version 3 of the License, or",
"// (at your option) any later version.",
"//",
"// This program is distributed in the hope that it will be useful,",
"// but WITHOUT ANY WARRANTY; without even the implied warranty of",
"// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the",
"// GNU Affero General Public License for more details.",
"//",
"// You should have received a copy of the GNU Affero General Public License",
"// along with this program. If not, see <https://www.gnu.org/licenses/>.",
""
]
}
}
71 changes: 71 additions & 0 deletions .tools/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# ebuild-updater

An automated ebuild updating system.

## Usage

Create an `updater.yml` file in the root of your repository. Create a
key for each package that should be managed by the updater.

```yaml
dev-util/mise:
resolver: git
options:
# url where the git repository lives. HTTPS is recommended.
url: https://github.com/jdx/mise
```
By default, if the only change that needs to be done during an upgrade
is a rename of the ebuild and regenerating the manifest, you're done!
However, if you require custom logic (e.g., running `pycargoebuild`),
you can specify steps to be ran during the upgrade process. An example
for `mise` (a rust ebuild) is shown below.

```yaml
dev-util/mise:
resolver: git
options:
url: https://github.com/jdx/mise
steps:
- command: git clone https://github.com/jdx/mise .
- original_ebuild: mise.ebuild
- command: pycargoebuild -i mise.ebuild
- ebuild: mise.ebuild
```

**Note**: All steps are ran inside of a Gentoo based docker image.

## Configuration File

| Key | Description |
| --- | --- |
| `resolver` | The resolver to use for the package. Valid options are `git` or `apt`. |
| `options` | Options for the resolver. |
| `steps` | Steps to be ran during the upgrade process. |

### **git** `options`

| Key | Description |
| --- | --- |
| `url` | The URL where the git repository lives. HTTPS is recommended. |

### **apt** `options`

| Key | Description |
| --- | --- |
| `repository` | Sources list entry for the APT repository |
| `package` | The package name as it appears in the repository |

### `steps`

| Key | Description |
| --- | --- |
| `command` | A command to be ran. |
| `original_ebuild` | Path to write an ebuild |
| `ebuild` | Path to read modified ebuild from |

## License

GPL-2.0
197 changes: 197 additions & 0 deletions .tools/cmd/elint/elint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
// Copyright (C) 2023 Jared Allard <[email protected]>
// Copyright (C) 2023 Outreach <https://outreach.io>
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License version
// 2 as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Package main implements a linter for Gentoo ebuilds. It is intended
// to be ran only on the jaredallard/asahi-overlay repository. It mainly
// handles:
// - Ensuring ebuilds have certain variables set.
// - Ensuring that Manifest files have been updated.
package main

import (
"bytes"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/egym-playground/go-prefix-writer/prefixer"
"github.com/fatih/color"
"github.com/jaredallard/overlay/.tools/internal/ebuild"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

// contains color helpers
var (
bold = color.New(color.Bold).SprintFunc()
faint = color.New(color.Faint).SprintFunc()
red = color.New(color.FgRed).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
yellow = color.New(color.FgYellow).SprintFunc()
)

// main is the entrypoint for the linter.
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

// lint lints the provided packageName in the provided workDir.
func lint(workDir, packageName string) (errOutput string) {
// packageName is the format of "category/package".
packageName = strings.TrimSuffix(packageName, "/")
packagePath := packageName

// find the first ebuild in the package directory.
files, err := os.ReadDir(packagePath)
if err != nil {
return "failed to read package directory"
}

var ebuildPath string
for _, file := range files {
if filepath.Ext(file.Name()) == ".ebuild" {
ebuildPath = filepath.Join(packagePath, file.Name())
break
}
}
if ebuildPath == "" {
return "no ebuild found in package directory"
}

e, err := ebuild.Parse(ebuildPath)
if err != nil {
return errors.Wrap(err, "failed to parse ebuild").Error()
}

if e.Description == "" {
return "ebuild: missing DESCRIPTION " + "(" + filepath.Base(ebuildPath) + ")"
}

if e.License == "" {
return "ebuild: missing LICENSE " + "(" + filepath.Base(ebuildPath) + ")"
}

// Validate that the Manifest file is up-to-date for the package.
var buf bytes.Buffer
out := prefixer.New(&buf, func() string { return color.New(color.Faint).Sprint(" => ") })
if err := ebuild.ValidateManifest(
out, out,
workDir,
packageName,
); err != nil {
errOutput = buf.String()
if errors.Is(err, ebuild.ErrManifestInvalid) {
errOutput += yellow("Manifest is out-of-date or otherwise invalid. Regenerate with 'ebuild <.ebuild> manifest'")
return
}

errOutput += "Manifest validation failed for an unknown reason (err: " + err.Error() + ")"
return
}

errOutput = ""
return
}

var rootCmd = &cobra.Command{
Use: "linter [packageName...]",
Short: "Ensures ebuilds pass lint checks as well as being valid.",
Long: "If no arguments are passed, all packages in the current directory will be linted.\n" +
"If arguments are passed, only those packages will be linted.",
Run: func(cmd *cobra.Command, args []string) {
workDir, err := os.Getwd()
if err != nil {
fmt.Fprintln(os.Stderr, "failed to get working directory:", err)
os.Exit(1)
}

if len(args) == 0 {
// If no arguments are passed, lint all packages in the current
// directory.
files, err := os.ReadDir(workDir)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to read directory:", err)
os.Exit(1)
}

for _, file := range files {
if !file.IsDir() {
continue
}

// skip hidden directories.
if strings.HasPrefix(file.Name(), ".") {
continue
}

// Skip non-package directories.
if file.Name() == "metadata" || file.Name() == "profiles" {
continue
}

subDir := filepath.Join(workDir, file.Name())
subFiles, err := os.ReadDir(subDir)
if err != nil {
fmt.Fprintln(os.Stderr, "failed to read directory", subDir+":", err)
os.Exit(1)
}
for _, subFile := range subFiles {
if !subFile.IsDir() {
continue
}

// join the subdirectory with the subdir to get the full
// package name (category/package).
args = append(args, filepath.Join(file.Name(), subFile.Name()))
}
}
}

if len(args) == 0 {
fmt.Fprintln(os.Stdout, "no packages to lint")
os.Exit(0)
}

if len(args) == 1 {
fmt.Println("Linting package", args[0])
} else {
fmt.Println("Linting all packages in the current directory")
}

for _, packageName := range args {
packageNameFaint := faint(packageName)
fmt.Print(packageNameFaint, bold(" ..."))

if err := lint(workDir, packageName); err != "" {
// update the line to be red.
fmt.Printf("\r%s %s\n", packageNameFaint, red("✘ "))

// print the error and then exit
fmt.Fprintln(os.Stderr, err)
fmt.Println("Linting failed for package", packageName)
os.Exit(1)
}

// update the line to be green.
fmt.Printf("\r%s %s\n", packageNameFaint, green("✔ "))
}

fmt.Println("All package(s) linted successfully")
},
}
Loading

0 comments on commit c0d6f59

Please sign in to comment.