-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
As a further experiment, switch to using writefs, which is copied by hand into this repo as an internal command. There is no speed advantage to using it, rather it is a usability and readability win. Kicking the tyres with it more in this repo will help to see whether the pattern works or not. Signed-off-by: Paul Jolly <[email protected]> Change-Id: Ia9203e2138ad74947ba49614df3298d84b7aa860 Dispatch-Trailer: {"type":"trybot","CL":1170145,"patchset":6,"ref":"refs/changes/45/1170145/6","targetBranch":"alpha"}
- Loading branch information
Showing
4 changed files
with
155 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2023 The CUE Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ci | ||
|
||
import ( | ||
"encoding/yaml" | ||
"strings" | ||
|
||
"github.com/cue-lang/cuelang.org/internal/ci/base" | ||
"github.com/cue-lang/cuelang.org/internal/ci/repo" | ||
"github.com/cue-lang/cuelang.org/internal/ci/github" | ||
_netlify "github.com/cue-lang/cuelang.org/internal/ci/netlify" | ||
) | ||
|
||
// #writefs mirrors the type of the arguments expected by | ||
// internal/cmd/writefs | ||
#writefs: { | ||
Remove: [...string] | ||
Create: [string]: { | ||
Type: "symlink" | *"file" | ||
Contents: *"" | string | ||
} | ||
} | ||
|
||
fs: #writefs & { | ||
Remove: [ | ||
"../../.github/workflows/*.yml", | ||
] | ||
|
||
Create: { | ||
[_]: { | ||
// TODO: do not hardcode this to ci_tool | ||
let donotedit = base.doNotEditMessage & {#generatedBy: "internal/ci/ci_tool.cue", _} | ||
_res: string | ||
Contents: "# \(donotedit)\n\n\(strings.TrimSpace(_res))\n" | ||
} | ||
for _name, _workflow in github.workflows { | ||
"../../.github/workflows/\(_name).yml": { | ||
_res: yaml.Marshal(_workflow) | ||
} | ||
} | ||
"../../codereview.cfg": { | ||
_res: base.toCodeReviewCfg & {#input: repo.codeReview, _} | ||
} | ||
"../../netlify.toml": { | ||
_res: _netlify.#toToml & {#input: _netlify.config, _} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
type file struct { | ||
Type string | ||
|
||
// If typ == "symlink" this is the filepath of the target | ||
// otherwise it is the Contents of the file | ||
Contents string | ||
} | ||
|
||
type args struct { | ||
// Remove is a list of globs of files to remove. File | ||
// removal happens before file creation. | ||
Remove []string | ||
|
||
// Create is a map of file path (in Unix format) to contents. | ||
Create map[string]file | ||
} | ||
|
||
func main() { | ||
var todo args | ||
dec := json.NewDecoder(os.Stdin) | ||
if err := dec.Decode(&todo); err != nil { | ||
log.Fatalf("failed to decode arguments from stdin: %v", err) | ||
} | ||
for _, glob := range todo.Remove { | ||
files, err := filepath.Glob(glob) | ||
if err != nil { | ||
log.Fatalf("failed to glob %q: %v", glob, err) | ||
} | ||
if len(files) == 0 { | ||
continue | ||
} | ||
for _, f := range files { | ||
if err := os.Remove(f); err != nil { | ||
log.Fatalf("failed to remove %s: %v", f, err) | ||
} | ||
} | ||
} | ||
for fp, f := range todo.Create { | ||
fp = filepath.FromSlash(fp) | ||
dir := filepath.Dir(fp) | ||
if err := os.MkdirAll(dir, 0o777); err != nil { | ||
log.Fatalf("failed to mkdir %s: %v", dir, err) | ||
} | ||
switch f.Type { | ||
case "symlink": | ||
target := filepath.FromSlash(f.Contents) | ||
actualTarget, err := os.Readlink(fp) | ||
if err == nil && actualTarget == target { | ||
continue | ||
} | ||
if err := os.Symlink(target, fp); err != nil { | ||
log.Fatalf("failed to symlink %s -> %s: %v", fp, target, err) | ||
} | ||
case "file": | ||
// If we have a .json file, normalise the contents first | ||
// as a special case. | ||
// | ||
// TODO: this actually belongs as an option in CUE. | ||
contents := []byte(f.Contents) | ||
if filepath.Ext(fp) == ".json" { | ||
var i any | ||
err := json.Unmarshal(contents, &i) | ||
if err == nil { | ||
var b bytes.Buffer | ||
enc := json.NewEncoder(&b) | ||
enc.SetEscapeHTML(false) | ||
enc.SetIndent("", " ") | ||
if err := enc.Encode(i); err == nil { | ||
// Add a trailing newline | ||
contents = b.Bytes() | ||
} | ||
} | ||
} | ||
if err := os.WriteFile(fp, []byte(contents), 0o666); err != nil { | ||
log.Fatalf("failed to write file %s: %v", fp, err) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters