Skip to content

Commit

Permalink
feat(snap): publishing to Snapcraft
Browse files Browse the repository at this point in the history
Close #3187
  • Loading branch information
develar committed Jul 10, 2019
1 parent 3bf2091 commit ff242ab
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 23 deletions.
13 changes: 13 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ edit_uri: ""
dev_addr: 0.0.0.0:8000
strict: true

extra_javascript:
- extra.js

markdown_extensions:
- admonition
- smarty
Expand Down
1 change: 1 addition & 0 deletions packages/app-builder-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export { PublishManager } from "./publish/PublishManager"
export { PlatformPackager } from "./platformPackager"
export { Framework, PrepareApplicationStageDirectoryOptions } from "./Framework"
export { buildForge, ForgeOptions } from "./forge-maker"
export { SnapStoreOptions } from "./publish/SnapStorePublisher"

const expectedOptions = new Set(["publish", "targets", "mac", "win", "linux", "projectDir", "platformPackagerFactory", "config", "effectiveOptionComputed", "prepackaged"])

Expand Down
2 changes: 1 addition & 1 deletion packages/app-builder-lib/src/packagerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export interface ArtifactCreated extends UploadTask {

readonly safeArtifactName?: string | null

readonly publishConfig?: PublishConfiguration
readonly publishConfig?: PublishConfiguration | null

readonly isWriteUpdateInfo?: boolean
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BintrayClient, Version } from "builder-util-runtime/out/bintray"
import { httpExecutor } from "builder-util/out/nodeHttpExecutor"
import { ClientRequest, RequestOptions } from "http"
import { Lazy } from "lazy-val"
import { HttpPublisher, PublishContext, PublishOptions } from "./publisher"
import { HttpPublisher, PublishContext, PublishOptions } from "electron-publish"

export class BintrayPublisher extends HttpPublisher {
private readonly _versionPromise = new Lazy(() => this.init())
Expand Down
10 changes: 7 additions & 3 deletions packages/app-builder-lib/src/publish/PublishManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Arch, asArray, AsyncTaskManager, InvalidConfigurationError, isEmptyOrSp
import { BintrayOptions, CancellationToken, GenericServerOptions, getS3LikeProviderBaseUrl, GithubOptions, githubUrl, PublishConfiguration, PublishProvider } from "builder-util-runtime"
import _debug from "debug"
import { getCiTag, PublishContext, Publisher, PublishOptions, UploadTask } from "electron-publish"
import { BintrayPublisher } from "electron-publish/out/BintrayPublisher"
import { BintrayPublisher } from "./BintrayPublisher"
import { GitHubPublisher } from "electron-publish/out/gitHubPublisher"
import { MultiProgress } from "electron-publish/out/multiProgress"
import S3Publisher from "electron-publish/out/s3/s3Publisher"
import SpacesPublisher from "electron-publish/out/s3/spacesPublisher"
import S3Publisher from "./s3/s3Publisher"
import SpacesPublisher from "./s3/spacesPublisher"
import { writeFile } from "fs-extra"
import isCi from "is-ci"
import * as path from "path"
Expand All @@ -18,6 +18,7 @@ import { Packager } from "../packager"
import { PlatformPackager } from "../platformPackager"
import { expandMacro } from "../util/macroExpander"
import { WinPackager } from "../winPackager"
import { SnapStoreOptions, SnapStorePublisher } from "./SnapStorePublisher"
import { createUpdateInfoTasks, UpdateInfoFileTask, writeUpdateInfoFiles } from "./updateInfoBuilder"

const publishForPrWarning = "There are serious security concerns with PUBLISH_FOR_PULL_REQUEST=true (see the CircleCI documentation (https://circleci.com/docs/1.0/fork-pr-builds/) for details)" +
Expand Down Expand Up @@ -277,6 +278,9 @@ export function createPublisher(context: PublishContext, version: string, publis
case "generic":
return null

case "snapStore":
return new SnapStorePublisher(context, publishConfig as SnapStoreOptions)

default:
const clazz = requireProviderClass(provider, packager)
return clazz == null ? null : new clazz(context, publishConfig)
Expand Down
49 changes: 49 additions & 0 deletions packages/app-builder-lib/src/publish/SnapStorePublisher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { Publisher, UploadTask, PublishContext } from "electron-publish"
import { executeAppBuilder } from "builder-util"
import * as path from "path"
import { PublishConfiguration } from "builder-util-runtime"

export class SnapStorePublisher extends Publisher {
readonly providerName = "snapStore"

constructor(context: PublishContext, private options: SnapStoreOptions) {
super(context)
}

upload(task: UploadTask): Promise<any> {
this.createProgressBar(path.basename(task.file), -1)

const args = ["publish-snap", "-f", task.file]

let channels = this.options.channels
if (channels == null) {
channels = ["edge"]
}
else {
if (typeof channels === "string") {
channels = channels.split(",")
}
}

for (const channel of channels) {
args.push("-c", channel)
}

return executeAppBuilder(args)
}

toString(): string {
return "Snap Store"
}
}

/**
* [Snap Store](https://snapcraft.io/) options.
*/
export interface SnapStoreOptions extends PublishConfiguration {
/**
* The list of channels the snap would be released.
* @default ["edge"]
*/
readonly channels?: string | Array<string> | null
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { log, executeAppBuilder } from "builder-util"
import { BaseS3Options } from "builder-util-runtime"
import { PublishContext, Publisher, UploadTask } from "../publisher"
import { PublishContext, Publisher, UploadTask } from "electron-publish"
import { ensureDir, symlink } from "fs-extra"
import * as path from "path"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { executeAppBuilder, InvalidConfigurationError, log } from "builder-util"
import { S3Options } from "builder-util-runtime"
import { PublishContext } from "../publisher"
import { PublishContext } from "electron-publish"
import { BaseS3Publisher } from "./BaseS3Publisher"

export default class S3Publisher extends BaseS3Publisher {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InvalidConfigurationError, isEmptyOrSpaces } from "builder-util"
import { SpacesOptions } from "builder-util-runtime"
import { PublishContext } from "../publisher"
import { PublishContext } from "electron-publish"
import { BaseS3Publisher } from "./BaseS3Publisher"

export default class SpacesPublisher extends BaseS3Publisher {
Expand Down
9 changes: 8 additions & 1 deletion packages/app-builder-lib/src/targets/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,14 @@ export default class SnapTarget extends Target {
}
await executeAppBuilder(args)

await packager.dispatchArtifactCreated(artifactPath, this, arch, packager.computeSafeArtifactName(artifactName, "snap", arch, false))
await packager.info.callArtifactBuildCompleted({
file: artifactPath,
safeArtifactName: packager.computeSafeArtifactName(artifactName, "snap", arch, false),
target: this,
arch,
packager,
publishConfig: options.publish == null ? {provider: "snapStore"} : null,
})
}

private isElectronVersionGreaterOrEqualThen(version: string) {
Expand Down
2 changes: 1 addition & 1 deletion packages/builder-util-runtime/src/CancellationToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,6 @@ export class CancellationToken extends EventEmitter {

export class CancellationError extends Error {
constructor() {
super("Cancelled")
super("cancelled")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class ProgressCallbackTransform extends Transform {

_transform(chunk: any, encoding: string, callback: any) {
if (this.cancellationToken.cancelled) {
callback(new Error("Cancelled"), null)
callback(new Error("cancelled"), null)
return
}

Expand All @@ -48,7 +48,7 @@ export class ProgressCallbackTransform extends Transform {

_flush(callback: any): void {
if (this.cancellationToken.cancelled) {
callback(new Error("Cancelled"))
callback(new Error("cancelled"))
return
}

Expand Down
2 changes: 1 addition & 1 deletion packages/builder-util-runtime/src/publishOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export type PublishProvider = "github" | "bintray" | "s3" | "spaces" | "generic" | "custom"
export type PublishProvider = "github" | "bintray" | "s3" | "spaces" | "generic" | "custom" | "snapStore"

// typescript-json-schema generates only PublishConfiguration if it is specified in the list, so, it is not added here
export type AllPublishOptions = string | GithubOptions | S3Options | SpacesOptions | GenericServerOptions | BintrayOptions | CustomPublishOptions
Expand Down
2 changes: 1 addition & 1 deletion packages/electron-publish/src/publisher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export abstract class Publisher {
}

export abstract class HttpPublisher extends Publisher {
constructor(protected readonly context: PublishContext, private readonly useSafeArtifactName = false) {
protected constructor(protected readonly context: PublishContext, private readonly useSafeArtifactName = false) {
super(context)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/electron-updater/src/AppUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ export abstract class AppUpdater extends EventEmitter {
await removeFileIfAny()

if (e instanceof CancellationError) {
log.info("Cancelled")
log.info("cancelled")
this.emit("update-cancelled", updateInfo)
}
throw e
Expand Down
2 changes: 1 addition & 1 deletion scripts/jsdoc/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function identifierToLink(id, root) {
id !== "module:https.RequestOptions" &&
!id.endsWith(".__type")
) {
for (const name of ["GithubOptions", "GenericServerOptions", "BintrayOptions", "S3Options", "SpacesOptions", "PublishConfiguration"]) {
for (const name of ["GithubOptions", "GenericServerOptions", "BintrayOptions", "S3Options", "SpacesOptions", "PublishConfiguration", "SnapStoreOptions"]) {
if (id.endsWith(`.${name}`)) {
return `[${name}](/configuration/publish#${name.toLowerCase()})`
}
Expand Down
6 changes: 2 additions & 4 deletions scripts/jsdoc2md.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,8 @@ async function render2(files, jsdoc2MdOptions) {
}),

new Page("generated/s3-options.md", "S3Options"),

new Page("generated/spaces-options.md", null, {
"SpacesOptions": "",
}),
new Page("generated/snap-store-options.md", null, {"SnapStoreOptions": ""}),
new Page("generated/spaces-options.md", null, {"SpacesOptions": ""}),

new Page("generated/appimage-options.md", "AppImageOptions"),
new Page("generated/DebOptions.md", "DebOptions"),
Expand Down
6 changes: 4 additions & 2 deletions scripts/snap-template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ rm -rf ~/squashfs-root/app ~/squashfs-root/snap ~/squashfs-root/meta ~/squashfs-
rm -rf ~/squashfs-root/etc ~/squashfs-root/var

#rm -f /home/develar/snap-template-electron-4.0.tar.7z && cd ~/squashfs-root && tar cf - . | zstd -22 --ultra --long -o ~/snap-template-electron-4.0.tar.zstd
rm -f /home/develar/snap-template-electron-4.0.tar.7z && cd ~/squashfs-root && tar cf - . | 7za a -mx=9 -mfb=64 -si ~/snap-template-electron-4.0.tar.7z
rm -f /home/develar/snap-template-electron-4.0.tar.7z && cd ~/squashfs-root && tar cf - . | 7za a -mx=9 -mfb=64 -si ~/snap-template-electron-4.0-2.tar.7z

zip -yX9 -r ~/snap-template-electron-4.0-2.zip .

#rm -f /home/develar/snap-template-electron-4.0.tar.7z && cd ~/squashfs-root && tar cf - . | 7za a -mx=9 -mfb=64 -si ~/snap-template-electron-4.0-1-arm.tar.7z

mv ~/snap-template-electron-4.0.tar.7z /media/psf/ramdisk/snap-template-electron-4.0.tar.7z
shasum -a 512 /Volumes/ramdisk/snap-template-electron-4.0.tar.7z | xxd -r -p | base64
shasum -a 512 /Volumes/ramdisk/snap-template-electron-4.0-2.tar.7z | xxd -r -p | base64
2 changes: 1 addition & 1 deletion test/src/ArtifactPublisherTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Arch, copyFile, TmpDir } from "builder-util"
import { CancellationToken, HttpError, S3Options, SpacesOptions } from "builder-util-runtime"
import { createPublisher } from "app-builder-lib/out/publish/PublishManager"
import { PublishContext } from "electron-publish"
import { BintrayPublisher } from "electron-publish/out/BintrayPublisher"
import { BintrayPublisher } from "app-builder-lib/out/publish/BintrayPublisher"
import { GitHubPublisher } from "electron-publish/out/gitHubPublisher"
import isCi from "is-ci"
import { join } from "path"
Expand Down

0 comments on commit ff242ab

Please sign in to comment.