• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright 2018 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
18	"bytes"
19	"fmt"
20	"os"
21	"os/exec"
22	"path/filepath"
23
24	cf "github.com/google/wuffs/cmd/commonflags"
25)
26
27func genrelease(wuffsRoot string, langs []string, v cf.Version) error {
28	revision := runGitCommand(wuffsRoot, "rev-parse", "HEAD")
29	commitDate := runGitCommand(wuffsRoot, "show",
30		"--quiet", "--date=format-local:%Y-%m-%d", "--format=%cd")
31	gitRevListCount := runGitCommand(wuffsRoot, "rev-list", "--count", "HEAD")
32	for _, lang := range langs {
33		filename, contents, err := genreleaseLang(wuffsRoot, revision, commitDate, gitRevListCount, v, lang)
34		if err != nil {
35			return err
36		}
37		if err := writeFile(filename, contents); err != nil {
38			return err
39		}
40	}
41	return nil
42}
43
44func genreleaseLang(wuffsRoot string, revision string, commitDate, gitRevListCount string, v cf.Version, lang string) (filename string, contents []byte, err error) {
45	qualFilenames, err := findFiles(filepath.Join(wuffsRoot, "gen", lang), "."+lang)
46	if err != nil {
47		return "", nil, err
48	}
49
50	command := "wuffs-" + lang
51	args := []string(nil)
52	args = append(args, "genrelease",
53		"-revision", revision,
54		"-commitdate", commitDate,
55		"-version", v.String(),
56	)
57	if gitRevListCount != "" {
58		args = append(args, "-gitrevlistcount", gitRevListCount)
59	}
60	args = append(args, qualFilenames...)
61	stdout := &bytes.Buffer{}
62
63	cmd := exec.Command(command, args...)
64	cmd.Stdout = stdout
65	cmd.Stderr = os.Stderr
66	if err := cmd.Run(); err == nil {
67		// No-op.
68	} else if _, ok := err.(*exec.ExitError); ok {
69		return "", nil, fmt.Errorf("%s: failed", command)
70	} else {
71		return "", nil, err
72	}
73
74	base := "wuffs-unsupported-snapshot"
75	if v.Major != 0 || v.Minor != 0 {
76		base = fmt.Sprintf("wuffs-v%d.%d", v.Major, v.Minor)
77	}
78	return filepath.Join(wuffsRoot, "release", lang, base+"."+lang), stdout.Bytes(), nil
79}
80
81func runGitCommand(wuffsRoot string, cmdArgs ...string) string {
82	cmd := exec.Command("git", cmdArgs...)
83	cmd.Dir = wuffsRoot
84	cmd.Env = []string{"TZ=UTC"}
85	out, err := cmd.Output()
86	if err != nil {
87		return ""
88	}
89	return string(bytes.TrimSpace(out))
90}
91