ALR
/
build.go
100 строк · 2.7 Кб
1/*
2* ALR - Any Linux Repository
3* Copyright (C) 2024 Евгений Храмов
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19package main20
21import (22"os"23"path/filepath"24
25"github.com/urfave/cli/v2"26"plemya-x.ru/alr/internal/config"27"plemya-x.ru/alr/internal/osutils"28"plemya-x.ru/alr/internal/types"29"plemya-x.ru/alr/pkg/build"30"plemya-x.ru/alr/pkg/loggerctx"31"plemya-x.ru/alr/pkg/manager"32"plemya-x.ru/alr/pkg/repos"33)
34
35var buildCmd = &cli.Command{36Name: "build",37Usage: "Build a local package",38Flags: []cli.Flag{39&cli.StringFlag{40Name: "script",41Aliases: []string{"s"},42Value: "alr.sh",43Usage: "Path to the build script",44},45&cli.StringFlag{46Name: "package",47Aliases: []string{"p"},48Usage: "Name of the package to build and its repo (example: default/go-bin)",49},50&cli.BoolFlag{51Name: "clean",52Aliases: []string{"c"},53Usage: "Build package from scratch even if there's an already built package available",54},55},56Action: func(c *cli.Context) error {57ctx := c.Context58log := loggerctx.From(ctx)59
60script := c.String("script")61if c.String("package") != "" {62script = filepath.Join(config.GetPaths(ctx).RepoDir, c.String("package"), "alr.sh")63}64
65err := repos.Pull(ctx, config.Config(ctx).Repos)66if err != nil {67log.Fatal("Error pulling repositories").Err(err).Send()68}69
70mgr := manager.Detect()71if mgr == nil {72log.Fatal("Unable to detect a supported package manager on the system").Send()73}74
75pkgPaths, _, err := build.BuildPackage(ctx, types.BuildOpts{76Script: script,77Manager: mgr,78Clean: c.Bool("clean"),79Interactive: c.Bool("interactive"),80})81if err != nil {82log.Fatal("Error building package").Err(err).Send()83}84
85wd, err := os.Getwd()86if err != nil {87log.Fatal("Error getting working directory").Err(err).Send()88}89
90for _, pkgPath := range pkgPaths {91name := filepath.Base(pkgPath)92err = osutils.Move(pkgPath, filepath.Join(wd, name))93if err != nil {94log.Fatal("Error moving the package").Err(err).Send()95}96}97
98return nil99},100}
101