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

     1  package dmg
     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/dmg"
    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-dmg"
    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, // macOS app ID to use
    37  	appName string, // Human-readable name for the app
    38  	pgpKey []byte, // PGP key contents
    39  	pgpKeyPassword string, // Password for the PGP key
    40  	packages []string, // MacPorts packages to install
    41  	releases []renderers.Release, // App releases
    42  	overwrite bool, // Overwrite files even if they exist
    43  	branchID, // Branch ID
    44  	branchName string, // Branch name
    45  	branchTimestamp time.Time, // Branch timestamp
    46  	goMain, // Directory with the main package to build
    47  	goFlags, // Flags to pass to the Go command
    48  	goGenerate string, // Command to execute go generate with
    49  ) *Builder {
    50  	return &Builder{
    51  		ctx,
    52  		cli,
    53  
    54  		image,
    55  		pull,
    56  		src,
    57  		dst,
    58  		onID,
    59  		stdout,
    60  		iconFilePath,
    61  		appID,
    62  		appName,
    63  		base64.StdEncoding.EncodeToString(pgpKey),
    64  		pgpKeyPassword,
    65  		packages,
    66  		releases,
    67  		overwrite,
    68  		branchID,
    69  		branchName,
    70  		branchTimestamp,
    71  		goMain,
    72  		goFlags,
    73  		goGenerate,
    74  	}
    75  }
    76  
    77  type Builder struct {
    78  	ctx context.Context
    79  	cli *client.Client
    80  
    81  	image string
    82  	pull  bool
    83  	src,
    84  	dst string
    85  	onID   func(id string)
    86  	stdout io.Writer
    87  	iconFilePath,
    88  	appID,
    89  	appName,
    90  	pgpKey,
    91  	pgpKeyPassword string
    92  	packages  []string
    93  	releases  []renderers.Release
    94  	overwrite bool
    95  	branchID,
    96  	branchName string
    97  	branchTimestamp time.Time
    98  	goMain,
    99  	goFlags,
   100  	goGenerate string
   101  }
   102  
   103  func (b *Builder) Render(workdir string, ejecting bool) error {
   104  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   105  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   106  
   107  	return utils.WriteRenders(
   108  		filepath.Join(workdir, b.goMain),
   109  		[]renderers.Renderer{
   110  			xdg.NewIconRenderer(
   111  				filepath.Join(workdir, b.goMain, b.iconFilePath),
   112  				"icon.icns",
   113  				utils.ImageTypeICNS,
   114  				512,
   115  				512,
   116  			),
   117  			dmg.NewInfoRenderer(
   118  				appID,
   119  				appName,
   120  				b.releases,
   121  			),
   122  		},
   123  		b.overwrite,
   124  		ejecting,
   125  	)
   126  }
   127  
   128  func (b *Builder) Build() error {
   129  	dst := builders.GetFilepathForBranch(b.dst, b.branchID)
   130  	appID := builders.GetAppIDForBranch(b.appID, b.branchID)
   131  	appName := builders.GetAppNameForBranch(b.appName, b.branchName)
   132  
   133  	return executors.DockerRunImage(
   134  		b.ctx,
   135  		b.cli,
   136  		b.image,
   137  		b.pull,
   138  		true,
   139  		b.src,
   140  		dst,
   141  		b.onID,
   142  		b.stdout,
   143  		map[string]string{
   144  			"APP_ID":                   appID,
   145  			"APP_NAME":                 appName,
   146  			"PGP_KEY":                  b.pgpKey,
   147  			"PGP_KEY_PASSWORD":         b.pgpKeyPassword,
   148  			"ARCHITECTURES":            "amd64 arm64",
   149  			"MACPORTS":                 strings.Join(b.packages, " "),
   150  			"GOMAIN":                   b.goMain,
   151  			"GOFLAGS":                  b.goFlags,
   152  			"GOGENERATE":               b.goGenerate,
   153  			"BRANCH_ID":                b.branchID,
   154  			"BRANCH_TIMESTAMP_RFC3339": fmt.Sprintf("%v", b.branchTimestamp.Format(time.RFC3339)),
   155  		},
   156  		b.Render,
   157  		[]string{},
   158  	)
   159  }