Skip to content

Commit

Permalink
Added new features
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan_ball committed May 29, 2019
1 parent 7b9d778 commit eb2dc6e
Show file tree
Hide file tree
Showing 7 changed files with 363 additions and 124 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion Launchd Package Creator/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// AppDelegate.swift
// simple-launchdaemon-creator
// launchd-package-creator
//
// Created by Ryan Ball on 4/11/19.
// Copyright © 2019 Ryan Ball. All rights reserved.
Expand Down
97 changes: 73 additions & 24 deletions Launchd Package Creator/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

227 changes: 183 additions & 44 deletions Launchd Package Creator/ViewController.swift

Large diffs are not rendered by default.

159 changes: 105 additions & 54 deletions Launchd Package Creator/create_daemon.swift
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//
// create_daemon.swift
// simple-launchdaemon-creator
// launchd-package-creator
//
// Created by Ryan Ball on 4/23/19.
// Copyright © 2019 Ryan Ball. All rights reserved.
//

// import Cocoa
import Foundation

var preferencesURL: URL!

struct Preferences: Codable {
// Items that are required in the plist
var Label: String
Expand All @@ -19,21 +20,36 @@ struct Preferences: Codable {
var StartInterval: Int?
var StandardOutPath: String?
var StandardErrorPath: String?
var LimitLoadToSessionType: String?
}

struct ComponentPlistRoot : Codable {
let array : [ComponentPlist]
}

public class create_daemon {
struct ComponentPlist: Codable {
var BundleIsRelocatable: Bool
var BundleIsVersionChecked: Bool
var BundleOverwriteAction: String
var RootRelativeBundlePath: String
}

public class create_daemon: NSObject {

var uuid: String = ""
var tempBuildDir: String = ""
var baseTempDir: URL!
var sessionTempDir: URL!
var componentPlistURL: URL!

public func build() {
public func build(buildType: String) {

uuid = NSUUID().uuidString
tempBuildDir = "com.github.ryangball.launchd-package-creator/\(uuid)"
baseTempDir = URL(fileURLWithPath: NSTemporaryDirectory())
sessionTempDir = baseTempDir.appendingPathComponent(tempBuildDir, isDirectory: true)
preferencesURL = sessionTempDir.appendingPathComponent("/root/Library/\(globalDaemonFolderName)/\(globalIdentifier).plist")
componentPlistURL = sessionTempDir.appendingPathComponent("/build/component.plist")

let subPaths = [
"root/Library/\(globalDaemonFolderName)",
Expand All @@ -53,21 +69,20 @@ public class create_daemon {
}

// Create the pkg postinstall script
let postInstallText = """
#!/bin/bash
# Set permissions on LaunchDaemon and Script
chown root:wheel "/Library/\(globalDaemonFolderName)/\(globalIdentifier).plist"
chmod 644 "/Library/\(globalDaemonFolderName)/\(globalIdentifier).plist"
chown -R root:wheel "/Library/Scripts/\(globalTargetPathFileName)"
chmod -R 755 "/Library/Scripts/\(globalTargetPathFileName)"
exit 0
"""

do {
// get the documents folder url
if let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
func createPostinstall() {
let postInstallText = """
#!/bin/bash
# Set permissions on LaunchDaemon and Script
chown root:wheel "/Library/\(globalDaemonFolderName)/\(globalIdentifier).plist"
chmod 644 "/Library/\(globalDaemonFolderName)/\(globalIdentifier).plist"
chown -R root:wheel "/Library/Scripts/\(globalTargetPathFileName)"
chmod -R 755 "/Library/Scripts/\(globalTargetPathFileName)"
exit 0
"""

do {
// create the destination url for the text file to be saved
let fileURL = sessionTempDir.appendingPathComponent("scripts/postinstall")

Expand All @@ -79,54 +94,90 @@ public class create_daemon {
attributes[.posixPermissions] = 0o755
do {
try FileManager.default.setAttributes(attributes, ofItemAtPath: fileURL.path)
}catch let error {
} catch let error {
print("Permissions error: ", error)
}

} catch {
print("error:", error)
}
} catch {
print("error:", error)
}

// Copy the target script/app
let destinationURL = sessionTempDir.appendingPathComponent("root/Library/Scripts/\(globalTargetPathFileName)")
do {
try FileManager.default.copyItem(atPath: globalTargetPath, toPath: destinationURL.path)
// func encodePlist(PlistData: String, Destination: URL) {
// let preferencesToEncode = ComponentPlist(BundleIsRelocatable: false, BundleIsVersionChecked: false, BundleOverwriteAction: "upgrade", RootRelativeBundlePath: "/Library/Scripts/\(globalTargetPathFileName)")
// let encoder = PropertyListEncoder()
// encoder.outputFormat = .xml
// do {
// let data = try encoder.encode(preferencesToEncode)
// try data.write(to: Destination)
// } catch {
// // Handle error
// print(error)
// }
// // Code goes here
// }

func createComponentPlist() {
let preferencesToEncode = ComponentPlist(BundleIsRelocatable: false, BundleIsVersionChecked: false, BundleOverwriteAction: "upgrade", RootRelativeBundlePath: "/Library/Scripts/\(globalTargetPathFileName)")
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do {
let data = try encoder.encode([preferencesToEncode].self)
try data.write(to: componentPlistURL)
} catch {
// Handle error
print(error)
}
}
catch let error as NSError {
print("Ooops! Something went wrong: \(error)")

// Copy the target script/app
func copyTarget() {
let destinationURL = sessionTempDir.appendingPathComponent("root/Library/Scripts/\(globalTargetPathFileName)")
_ = extras().copyFile(source: globalTargetPath, destination: destinationURL.path)
}

// Create the plist
let preferencesURL = sessionTempDir.appendingPathComponent("/root/Library/\(globalDaemonFolderName)/\(globalIdentifier).plist")
let preferencesToEncode = Preferences(Label: globalIdentifier, ProgramArguments: globalProgramArgsFull, RunAtLoad: globalRunAtLoad, StartInterval: globalStartInterval, StandardOutPath: globalStandardOutPath, StandardErrorPath: globalStandardErrorPath)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do {
let data = try encoder.encode(preferencesToEncode)
try data.write(to: preferencesURL)
} catch {
// Handle error
print(error)
func createPlist() {
let preferencesToEncode = Preferences(Label: globalIdentifier, ProgramArguments: globalProgramArgsFull, RunAtLoad: globalRunAtLoad, StartInterval: globalStartInterval, StandardOutPath: globalStandardOutPath, StandardErrorPath: globalStandardErrorPath, LimitLoadToSessionType: globalSessionType)
let encoder = PropertyListEncoder()
encoder.outputFormat = .xml
do {
let data = try encoder.encode(preferencesToEncode)
try data.write(to: preferencesURL)
} catch {
// Handle error
print(error)
}
}

// Create the .pkg
@discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
// Create the .pkg using shell commands
func createPKG() {
@discardableResult
func shell(_ args: String...) -> Int32 {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}

let pkgRoot = sessionTempDir.appendingPathComponent("/root/")
let pkgScripts = sessionTempDir.appendingPathComponent("/scripts/")
let pkgBuildDir = sessionTempDir.appendingPathComponent("build/")
globalPkgTempLocation = "\(pkgBuildDir.path)/hello.pkg"

shell("/usr/bin/pkgbuild", "--quiet", "--root", "\(pkgRoot.path)", "--install-location", "/", "--scripts", "\(pkgScripts.path)", "--identifier", "\(globalIdentifier)", "--version", "\(globalVersion)", "--ownership", "recommended", "--component-plist", "\(componentPlistURL.path)", "\(globalPkgTempLocation)")
}

let pkgRoot = sessionTempDir.appendingPathComponent("/root/")
let pkgScripts = sessionTempDir.appendingPathComponent("/scripts/")
let pkgBuildDir = sessionTempDir.appendingPathComponent("build/")
globalPkgTempLocation = "\(pkgBuildDir.path)/hello.pkg"

if buildType == "PKG" {
createPostinstall()
createComponentPlist()
copyTarget()
createPlist()
createPKG()
} else if buildType == "Plist" {
createPlist()
}

shell("/usr/bin/pkgbuild", "--quiet", "--root", "\(pkgRoot.path)", "--install-location", "/", "--scripts", "\(pkgScripts.path)", "--identifier", "\(globalIdentifier)", "--version", "\(globalVersion)", "--ownership", "recommended", "\(globalPkgTempLocation)")
}
}
2 changes: 1 addition & 1 deletion Launchd Package Creator/extras.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// dialog.swift
// testing
// launchd-package-creator
//
// Created by Ryan Ball on 4/15/19.
// Copyright © 2019 Ryan Ball. All rights reserved.
Expand Down
Binary file modified images/main_window.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit eb2dc6e

Please sign in to comment.