Skip to content

Commit

Permalink
fix: forget to package README into bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Nov 20, 2024
1 parent d02d6ea commit 04da3a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
26 changes: 24 additions & 2 deletions internal/core/bundle_packager/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ import (
type GenericBundlePackager struct {
bundle *bundle_entities.Bundle
assets map[string]*bytes.Buffer

extraFiles map[string]*bytes.Buffer
}

func NewGenericBundlePackager(bundle *bundle_entities.Bundle) *GenericBundlePackager {
return &GenericBundlePackager{bundle: bundle, assets: make(map[string]*bytes.Buffer)}
func NewGenericBundlePackager(
bundle *bundle_entities.Bundle,
extraFiles map[string]*bytes.Buffer,
) *GenericBundlePackager {
return &GenericBundlePackager{
bundle: bundle,
assets: make(map[string]*bytes.Buffer),
extraFiles: extraFiles,
}
}

func (p *GenericBundlePackager) Export() ([]byte, error) {
Expand Down Expand Up @@ -53,6 +62,19 @@ func (p *GenericBundlePackager) Export() ([]byte, error) {
}
}

// write the extra files
for name, file := range p.extraFiles {
extraFile, err := zipWriter.Create(name)
if err != nil {
return nil, err
}

_, err = extraFile.Write(file.Bytes())
if err != nil {
return nil, err
}
}

// close the zip writer to flush the buffer
zipWriter.Close()

Expand Down
19 changes: 18 additions & 1 deletion internal/core/bundle_packager/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,25 @@ func NewLocalBundlePackager(path string) (BundlePackager, error) {
return nil, err
}

// collect files starts with README
readmeFiles, err := filepath.Glob(filepath.Join(path, "README*"))
if err != nil {
return nil, err
}

extraFiles := make(map[string]*bytes.Buffer)
for _, readmeFile := range readmeFiles {
// trim the path
buffer, err := os.ReadFile(readmeFile)
if err != nil {
return nil, err
}
readmeFile = strings.TrimPrefix(readmeFile, filepath.Clean(path)+"/")
extraFiles[readmeFile] = bytes.NewBuffer(buffer)
}

packager := &LocalBundlePackager{
GenericBundlePackager: *NewGenericBundlePackager(&bundle),
GenericBundlePackager: *NewGenericBundlePackager(&bundle, extraFiles),
path: path,
}

Expand Down
18 changes: 17 additions & 1 deletion internal/core/bundle_packager/memory_zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,24 @@ func NewMemoryZipBundlePackager(zipFile []byte) (*MemoryZipBundlePackager, error
return nil, err
}

// collect files starts with README
extraFiles := make(map[string]*bytes.Buffer)
for _, file := range zipReader.File {
if strings.HasPrefix(file.Name, "README") {
buffer := bytes.NewBuffer([]byte{})
readFile, err := file.Open()
if err != nil {
return nil, err
}
defer readFile.Close()

io.Copy(buffer, readFile)
extraFiles[file.Name] = buffer
}
}

packager := &MemoryZipBundlePackager{
GenericBundlePackager: *NewGenericBundlePackager(&bundle),
GenericBundlePackager: *NewGenericBundlePackager(&bundle, extraFiles),
zipReader: zipReader,
}

Expand Down

0 comments on commit 04da3a0

Please sign in to comment.