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

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