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

     1  package msi
     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/msi"
    17  	"github.com/pojntfx/hydrapp/hydrapp/pkg/renderers/xdg"
    18  	"github.com/pojntfx/hydrapp/hydrapp/pkg/utils"
    19  )
    20  
    21  const (
    22  	Image = "ghcr.io/pojntfx/hydrapp-build-msi"
    23  )
    24  
    25  func NewBuilder(
    26  	ctx context.Context,
    27  	cli *client.Client,
    28  
    29  	image string, // OCI image to use
    30  	pull bool, // Whether to pull the image or not
    31  	src, // Input directory
    32  	dst string, // Output directory
    33  	onID func(id string), // Callback to handle container ID
    34  	stdout io.Writer, // Writer to handle container output
    35  	iconFilePath, // Path to icon to use
    36  	appID, // Android app ID to use
    37  	appName string, // Human-readable name for the app,
    38  	pgpKey []byte, // PGP key contents
    39  	pgpKeyPassword, // Password for the PGP key
    40  	architecture string, // Architecture to build for
    41  	packages []string, // MSYS2 packages to install. Only supported for amd64.
    42  	releases []renderers.Release, // App releases
    43  	overwrite bool, // Overwrite files even if they exist
    44  	branchID, // Branch ID
    45  	branchName string, // Branch name
    46  	branchTimestamp time.Time, // Branch timestamp
    47  	goMain, // Directory with the main package to build
    48  	goFlags, // Flags to pass to the Go command
    49  	include, // Regex of files and directories from MSYS2 to include
    50  	goGenerate string, // Command to execute go generate with
    51  ) *Builder {
    52  	return &Builder{
    53  		ctx,
    54  		cli,
    55  
    56  		image,
    57  		pull,
    58  		src,
    59  		dst,
    60  		onID,
    61  		stdout,
    62  		iconFilePath,
    63  		appID,
    64  		appName,
    65  		base64.StdEncoding.EncodeToString(pgpKey),
    66  		pgpKeyPassword,
    67  		architecture,
    68  		packages,
    69  		releases,
    70  		overwrite,
    71  		branchID,
    72  		branchName,
    73  		branchTimestamp,
    74  		goMain,
    75  		goFlags,
    76  		include,
    77  		goGenerate,
    78  	}
    79  }
    80  
    81  type Builder struct {
    82  	ctx context.Context
    83  	cli *client.Client
    84  
    85  	image string
    86  	pull  bool
    87  	src,
    88  	dst string
    89  	onID   func(id string)
    90  	stdout io.Writer
    91  	iconFilePath,
    92  	appID,
    93  	appName,
    94  	pgpKey,
    95  	pgpKeyPassword,
    96  	architecture string
    97  	packages  []string
    98  	releases  []renderers.Release
    99  	overwrite bool
   100  	branchID,
   101  	branchName string
   102  	branchTimestamp time.Time
   103  	goMain,
   104  	goFlags,
   105  	include,
   106  	goGenerate string
   107  }
   108  
   109  func (b *Builder) Render(workdir string, ejecting bool) error {
   110  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   111  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   112  
   113  	return utils.WriteRenders(
   114  		filepath.Join(workdir, b.goMain),
   115  		[]renderers.Renderer{
   116  			xdg.NewIconRenderer(
   117  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   118  				"icon.ico",
   119  				utils.ImageTypeICO,
   120  				256,
   121  				256,
   122  			),
   123  			msi.NewWixRenderer(
   124  				appID,
   125  				appName,
   126  				b.releases,
   127  			),
   128  		},
   129  		b.overwrite,
   130  		ejecting,
   131  	)
   132  }
   133  
   134  func (b *Builder) Build() error {
   135  	dst := builders.GetFilepathForBranch(b.dst, b.branchID)
   136  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   137  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   138  
   139  	return executors.DockerRunImage(
   140  		b.ctx,
   141  		b.cli,
   142  		b.image,
   143  		b.pull,
   144  		false,
   145  		b.src,
   146  		dst,
   147  		b.onID,
   148  		b.stdout,
   149  		map[string]string{
   150  			"APP_ID":                   appID,
   151  			"APP_NAME":                 appName,
   152  			"PGP_KEY":                  b.pgpKey,
   153  			"PGP_KEY_PASSWORD":         b.pgpKeyPassword,
   154  			"ARCHITECTURE":             b.architecture,
   155  			"MSYS2PACKAGES":            strings.Join(b.packages, " "),
   156  			"GOMAIN":                   b.goMain,
   157  			"GOFLAGS":                  b.goFlags,
   158  			"GOGENERATE":               b.goGenerate,
   159  			"MSYS2INCLUDE":             b.include,
   160  			"BRANCH_ID":                b.branchID,
   161  			"BRANCH_TIMESTAMP_RFC3339": fmt.Sprintf("%v", b.branchTimestamp.Format(time.RFC3339)),
   162  		},
   163  		b.Render,
   164  		[]string{},
   165  	)
   166  }