github.com/pojntfx/hydrapp/hydrapp@v0.0.0-20240516002902-d08759d6ca9f/pkg/builders/rpm/builder.go (about)

     1  package rpm
     2  
     3  import (
     4  	"context"
     5  	"encoding/base64"
     6  	"fmt"
     7  	"io"
     8  	"path/filepath"
     9  	"time"
    10  
    11  	"github.com/docker/docker/client"
    12  	"github.com/pojntfx/hydrapp/hydrapp/pkg/builders"
    13  	"github.com/pojntfx/hydrapp/hydrapp/pkg/executors"
    14  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers"
    15  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers/rpm"
    16  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers/xdg"
    17  	"github.com/pojntfx/hydrapp/hydrapp/pkg/utils"
    18  )
    19  
    20  const (
    21  	Image = "ghcr.io/pojntfx/hydrapp-build-rpm"
    22  )
    23  
    24  func NewBuilder(
    25  	ctx context.Context,
    26  	cli *client.Client,
    27  
    28  	image string, // OCI image to use
    29  	pull bool, // Whether to pull the image or not
    30  	src, // Input directory
    31  	dst string, // Output directory
    32  	onID func(id string), // Callback to handle container ID
    33  	stdout io.Writer, // Writer to handle container output
    34  	iconFilePath, // Path to icon to use
    35  	appID string, // RPM app ID to use
    36  	pgpKey []byte, // PGP key contents
    37  	pgpKeyPassword, // Password for the PGP key
    38  	pgpKeyID, // ID of the PGP key to use
    39  	baseURL, // Base URL where the repo is to be hosted
    40  	distro, // Distro to build for
    41  	architecture, // Architecture to build for
    42  	packageSuffix, // RPM package suffix
    43  	appName, // App name
    44  	appDescription, // App description
    45  	appSummary, // App summary
    46  	appURL, // App URL
    47  	appSPDX string, // App SPDX license identifier
    48  	releases []renderers.Release, // App releases
    49  	extraPackages []rpm.Package, // Extra RPM packages
    50  	overwrite bool, // Overwrite files even if they exist
    51  	branchID, // Branch ID
    52  	branchName string, // Branch name
    53  	branchTimestamp time.Time, // Branch timestamp
    54  	goMain, // Directory with the main package to build
    55  	goFlags, // Flags to pass to the Go command
    56  	goGenerate string, // Command to execute go generate with
    57  ) *Builder {
    58  	return &Builder{
    59  		ctx,
    60  		cli,
    61  
    62  		image,
    63  		pull,
    64  		src,
    65  		dst,
    66  		onID,
    67  		stdout,
    68  		iconFilePath,
    69  		appID,
    70  		base64.StdEncoding.EncodeToString(pgpKey),
    71  		pgpKeyPassword,
    72  		pgpKeyID,
    73  		baseURL,
    74  		distro,
    75  		architecture,
    76  		packageSuffix,
    77  		appName,
    78  		appDescription,
    79  		appSummary,
    80  		appURL,
    81  		appSPDX,
    82  		releases,
    83  		extraPackages,
    84  		overwrite,
    85  		branchID,
    86  		branchName,
    87  		branchTimestamp,
    88  		goMain,
    89  		goFlags,
    90  		goGenerate,
    91  	}
    92  }
    93  
    94  type Builder struct {
    95  	ctx context.Context
    96  	cli *client.Client
    97  
    98  	image string
    99  	pull  bool
   100  	src,
   101  	dst string
   102  	onID   func(id string)
   103  	stdout io.Writer
   104  	iconFilePath,
   105  	appID,
   106  	pgpKey,
   107  	pgpKeyPassword,
   108  	pgpKeyID,
   109  	baseURL,
   110  	distro,
   111  	architecture,
   112  	packageSuffix,
   113  	appName,
   114  	appDescription,
   115  	appSummary,
   116  	appURL,
   117  	appSPDX string
   118  	releases      []renderers.Release
   119  	extraPackages []rpm.Package
   120  	overwrite     bool
   121  	branchID,
   122  	branchName string
   123  	branchTimestamp time.Time
   124  	goMain,
   125  	goFlags,
   126  	goGenerate string
   127  }
   128  
   129  func (b *Builder) Render(workdir string, ejecting bool) error {
   130  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   131  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   132  
   133  	return utils.WriteRenders(
   134  		filepath.Join(workdir, b.goMain),
   135  		[]renderers.Renderer{
   136  			xdg.NewIconRenderer(
   137  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   138  				"icon-16x16.png",
   139  				utils.ImageTypePNG,
   140  				16,
   141  				16,
   142  			),
   143  			xdg.NewIconRenderer(
   144  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   145  				"icon-22x22.png",
   146  				utils.ImageTypePNG,
   147  				22,
   148  				22,
   149  			),
   150  			xdg.NewIconRenderer(
   151  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   152  				"icon-24x24.png",
   153  				utils.ImageTypePNG,
   154  				24,
   155  				24,
   156  			),
   157  			xdg.NewIconRenderer(
   158  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   159  				"icon-32x32.png",
   160  				utils.ImageTypePNG,
   161  				32,
   162  				32,
   163  			),
   164  			xdg.NewIconRenderer(
   165  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   166  				"icon-36x36.png",
   167  				utils.ImageTypePNG,
   168  				36,
   169  				36,
   170  			),
   171  			xdg.NewIconRenderer(
   172  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   173  				"icon-48x48.png",
   174  				utils.ImageTypePNG,
   175  				48,
   176  				48,
   177  			),
   178  			xdg.NewIconRenderer(
   179  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   180  				"icon-64x64.png",
   181  				utils.ImageTypePNG,
   182  				64,
   183  				64,
   184  			),
   185  			xdg.NewIconRenderer(
   186  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   187  				"icon-72x72.png",
   188  				utils.ImageTypePNG,
   189  				72,
   190  				72,
   191  			),
   192  			xdg.NewIconRenderer(
   193  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   194  				"icon-96x96.png",
   195  				utils.ImageTypePNG,
   196  				96,
   197  				96,
   198  			),
   199  			xdg.NewIconRenderer(
   200  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   201  				"icon-128x128.png",
   202  				utils.ImageTypePNG,
   203  				128,
   204  				128,
   205  			),
   206  			xdg.NewIconRenderer(
   207  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   208  				"icon-192x192.png",
   209  				utils.ImageTypePNG,
   210  				192,
   211  				192,
   212  			),
   213  			xdg.NewIconRenderer(
   214  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   215  				"icon-256x256.png",
   216  				utils.ImageTypePNG,
   217  				256,
   218  				256,
   219  			),
   220  			xdg.NewIconRenderer(
   221  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   222  				"icon-512x512.png",
   223  				utils.ImageTypePNG,
   224  				512,
   225  				512,
   226  			),
   227  			xdg.NewDesktopRenderer(
   228  				appID,
   229  				appName,
   230  				b.appDescription,
   231  			),
   232  			xdg.NewMetainfoRenderer(
   233  				appID,
   234  				appName,
   235  				b.appDescription,
   236  				b.appSummary,
   237  				b.appSPDX,
   238  				b.appURL,
   239  				b.releases,
   240  			),
   241  			rpm.NewSpecRenderer(
   242  				appID,
   243  				appName,
   244  				b.appDescription,
   245  				b.appSummary,
   246  				b.appSPDX,
   247  				b.appURL,
   248  				b.releases,
   249  				b.extraPackages,
   250  				b.goMain,
   251  				b.goFlags,
   252  				b.goGenerate,
   253  				b.branchTimestamp,
   254  			),
   255  		},
   256  		b.overwrite,
   257  		ejecting,
   258  	)
   259  }
   260  
   261  func (b *Builder) Build() error {
   262  	dst := builders.GetFilepathForBranch(b.dst, b.branchID)
   263  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   264  	baseURL := builders.GetPathForBranch(b.baseURL, b.branchID, "")
   265  
   266  	return executors.DockerRunImage(
   267  		b.ctx,
   268  		b.cli,
   269  		b.image,
   270  		b.pull,
   271  		true,
   272  		b.src,
   273  		dst,
   274  		b.onID,
   275  		b.stdout,
   276  		map[string]string{
   277  			"APP_ID":                appID,
   278  			"PGP_KEY":               b.pgpKey,
   279  			"PGP_KEY_PASSWORD":      b.pgpKeyPassword,
   280  			"PGP_KEY_ID":            b.pgpKeyID,
   281  			"BASE_URL":              baseURL,
   282  			"DISTRO":                b.distro,
   283  			"ARCHITECTURE":          b.architecture,
   284  			"PACKAGE_VERSION":       b.releases[0].Version,
   285  			"PACKAGE_SUFFIX":        b.packageSuffix,
   286  			"GOMAIN":                b.goMain,
   287  			"BRANCH_TIMESTAMP_UNIX": fmt.Sprintf("%v", b.branchTimestamp.Unix()),
   288  		},
   289  		b.Render,
   290  		[]string{},
   291  	)
   292  }