Skip to content

Commit

Permalink
Fix #726 Remove deperecation warnings. Manual cherry-pick of a1aefb4
Browse files Browse the repository at this point in the history
  • Loading branch information
muuki88 committed Nov 12, 2016
1 parent 55b3e60 commit 5d958ce
Showing 1 changed file with 82 additions and 58 deletions.
140 changes: 82 additions & 58 deletions src/main/scala/com/typesafe/sbt/packager/windows/WixHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,61 +8,71 @@ import sbt._
import collection.mutable.ArrayBuffer

case class WindowsProductInfo(
id: String, // UUID of the package
title: String, // Human readable name of the package
version: String, // Windows version
maintainer: String,
description: String,
upgradeId: String, // UUID for upgrading
comments: String = "",
installScope: String = "perMachine",
installerVersion: String = "200",
compressed: Boolean = true)
id: String, // UUID of the package
title: String, // Human readable name of the package
version: String, // Windows version
maintainer: String,
description: String,
upgradeId: String, // UUID for upgrading
comments: String = "",
installScope: String = "perMachine",
installerVersion: String = "200",
compressed: Boolean = true)

sealed trait FeatureComponent

/** Define a new feature, that will be selectable in the default MSI. */
case class WindowsFeature(
id: String,
title: String,
desc: String,
absent: String = "allow",
level: String = "1",
display: String = "collapse",
components: Seq[FeatureComponent] = Seq.empty) extends FeatureComponent {}
case class WindowsFeature(id: String,
title: String,
desc: String,
absent: String = "allow",
level: String = "1",
display: String = "collapse",
components: Seq[FeatureComponent] = Seq.empty)
extends FeatureComponent {}

/** Adds a file into a given windows feature. */
case class ComponentFile(
source: String,
editable: Boolean = false) extends FeatureComponent
case class ComponentFile(source: String, editable: Boolean = false)
extends FeatureComponent

/**
* Will add the directory to the windows path. NOTE: Only one of these
* per MSI.
*/
* Will add the directory to the windows path. NOTE: Only one of these
* per MSI.
*/
case class AddDirectoryToPath(dir: String = "") extends FeatureComponent
case class AddShortCuts(
target: Seq[String],
workingDir: String = "INSTALLDIR") extends FeatureComponent
case class AddShortCuts(target: Seq[String], workingDir: String = "INSTALLDIR")
extends FeatureComponent

// TODO - Shortcut as a component element.

/** Helper functions to deal with Wix/CAB craziness. */
object WixHelper {

/** Generates a windows friendly GUID for use in random locations in the build. */
def makeGUID: String = java.util.UUID.randomUUID.toString

// TODO - Fragment out this function a bit so it's not so ugly/random.
def makeWixProductConfig(name: String, product: WindowsProductInfo, features: Seq[WindowsFeature], license: Option[File] = None): scala.xml.Node = {
// TODO - Fragment out this function a bit so it's not so ugly/random.
def makeWixProductConfig(name: String,
product: WindowsProductInfo,
features: Seq[WindowsFeature],
license: Option[File] = None): scala.xml.Node = {
// TODO - First we find directories...
// Adds all subdirectories... there was a bug when there were directories with only subdirs and no files in the tree,
// so there was a gap and dirXml failed to create some directories
def allParentDirs(f: File): Seq[File] = Option(f).toSeq.flatMap(f => f +: allParentDirs(f.getParentFile))
def allParentDirs(f: File): Seq[File] =
Option(f).toSeq.flatMap(f => f +: allParentDirs(f.getParentFile))
val filenamesPrep =
for {
f <- features
ComponentFile(name, _) <- f.components
} yield allParentDirs(file(name))
val filenames = filenamesPrep.flatten.map(_.toString.replaceAll("\\\\", "/")).filter(_ != "")
val filenames = filenamesPrep.flatten
.map(_.toString.replaceAll("\\\\", "/"))
.filter(_ != "")
// Now for directories...
def parentDir(filename: String) = { filename take (filename lastIndexOf '/') }
def parentDir(filename: String) = {
filename take (filename lastIndexOf '/')
}
def simpleName(filename: String) = {
val lastSlash =
if (filename contains '/') filename lastIndexOf '/'
Expand All @@ -72,22 +82,25 @@ object WixHelper {
val dirs = (filenames map parentDir).distinct;
// Now we need our directory tree xml?
val dirToChildren = dirs groupBy parentDir;
def dirXml(currentDir: String): scala.xml.Node = if (!currentDir.isEmpty) {
val children = dirToChildren.getOrElse(currentDir, Seq.empty)
<Directory Id={ cleanStringForId(currentDir) } Name={ simpleName(currentDir) }>
def dirXml(currentDir: String): scala.xml.Node =
if (!currentDir.isEmpty) {
val children = dirToChildren.getOrElse(currentDir, Seq.empty)
<Directory Id={ cleanStringForId(currentDir) } Name={ simpleName(currentDir) }>
{
children map dirXml
}
</Directory>
} else <!-- -->
} else <!-- -->

// We need component helpers...
case class ComponentInfo(id: String, xml: scala.xml.Node)
def makeComponentInfo(c: FeatureComponent): ComponentInfo = c match {
case w: WindowsFeature => sys.error("Nested windows features currently unsupported!")
case w: WindowsFeature =>
sys.error("Nested windows features currently unsupported!")
case AddDirectoryToPath(dir) =>
val dirRef = if (dir.isEmpty) "INSTALLDIR" else cleanStringForId(dir)
val homeEnvVar = archetypes.JavaAppBatScript.makeEnvFriendlyName(name) + "_HOME"
val homeEnvVar = archetypes.JavaAppBatScript
.makeEnvFriendlyName(name) + "_HOME"
val pathAddition =
if (dir.isEmpty) "%" + homeEnvVar + "%"
else "[INSTALLDIR]" + dir.replaceAll("\\/", "\\\\")
Expand All @@ -104,7 +117,10 @@ object WixHelper {
ComponentInfo(id, xml)
case ComponentFile(name, editable) =>
val uname = name.replaceAll("\\\\", "/")
val dir = parentDir(uname).replaceAll("//", "/").stripSuffix("/").stripSuffix("/")
val dir = parentDir(uname)
.replaceAll("//", "/")
.stripSuffix("/")
.stripSuffix("/")
val dirRef = if (dir.isEmpty) "INSTALLDIR" else cleanStringForId(dir)
val fname = simpleName(uname)
val id = cleanStringForId(uname).takeRight(67) // Room for "fl_"
Expand All @@ -131,7 +147,8 @@ object WixHelper {
// that we remove all menu items.
case AddShortCuts(targets, workingDir) =>
val targetSize = targets.size.toString.size
val id = cleanStringForId("shortcut_" + makeGUID).takeRight(67 - targetSize) // Room for "_SC"+incremental number
val id = cleanStringForId("shortcut_" + makeGUID)
.takeRight(67 - targetSize) // Room for "_SC"+incremental number
val xml =
<DirectoryRef Id="ApplicationProgramsFolder">
<Component Id={ id } Guid={ makeGUID }>
Expand Down Expand Up @@ -202,10 +219,9 @@ object WixHelper {
</xml:group>
}

def makeWixConfig(
name: String, // package name
product: WindowsProductInfo,
rest: xml.Node): xml.Node = {
def makeWixConfig(name: String, // package name
product: WindowsProductInfo,
rest: xml.Node): xml.Node = {
<Wix xmlns='http://schemas.microsoft.com/wix/2006/wi' xmlns:util='http://schemas.microsoft.com/wix/UtilExtension'>
<Product Id={ product.id } Name={ product.title } Language='1033' Version={ product.version } Manufacturer={ product.maintainer } UpgradeCode={ product.upgradeId }>
<Package Description={ product.description } Comments={ product.comments } Manufacturer={ product.maintainer } InstallScope={ product.installScope } InstallerVersion={ product.installerVersion } Compressed={ if (product.compressed) "yes" else "no" }/>
Expand All @@ -216,31 +232,39 @@ object WixHelper {
}

/**
* Modifies a string to be Wix ID friendly by removing all the bad
* characters and replacing with _. Also limits the width to 70 (rather than
* 72) so we can safely add a few later.
*/
* Modifies a string to be Wix ID friendly by removing all the bad
* characters and replacing with _. Also limits the width to 70 (rather than
* 72) so we can safely add a few later.
*/
def cleanStringForId(n: String) = {
n.replaceAll("[^0-9a-zA-Z_]", "_").takeRight(60) + (math.abs(n.hashCode).toString + "xxxxxxxxx").substring(0, 9)
n.replaceAll("[^0-9a-zA-Z_]", "_")
.takeRight(60) + (math.abs(n.hashCode).toString + "xxxxxxxxx")
.substring(0, 9)
}

/** Cleans a file name for the Wix pre-processor. Every $ should be doubled. */
def cleanFileName(n: String) = {
n.replaceAll("\\$", "\\$\\$").replaceAll("\\/", "\\\\")
}

/** Takes a file and generates an ID for it. */
def makeIdFromFile(f: File) = cleanStringForId(f.getName)

/**
* Constructs a set of componentRefs and the directory/file WIX for
* all files in a given directory.
*
* @return A tuple where the first item is all the Component Ids created,
* and the second is the Directory/File/Component XML.
*/
@deprecated("Use higher level abstraction", "6/28/13")
def generateComponentsAndDirectoryXml(dir: File, id_prefix: String = ""): (Seq[String], scala.xml.Node) = {
def makeId(f: File) = cleanStringForId(IO.relativize(dir, f) map (id_prefix+) getOrElse (id_prefix + f.getName))
* Constructs a set of componentRefs and the directory/file WIX for
* all files in a given directory.
*
* @return A tuple where the first item is all the Component Ids created,
* and the second is the Directory/File/Component XML.
*/
// TODO @deprecated("Use higher level abstraction", "6/28/13")
// reference: https://github.com/sbt/sbt-native-packager/issues/726
def generateComponentsAndDirectoryXml(
dir: File,
id_prefix: String = ""): (Seq[String], scala.xml.Node) = {
def makeId(f: File) =
cleanStringForId(IO
.relativize(dir, f) map (id_prefix +) getOrElse (id_prefix + f.getName))
def handleFile(f: File): (Seq[String], scala.xml.Node) = {
val id = makeId(f)
val xml = (
Expand Down

0 comments on commit 5d958ce

Please sign in to comment.