Source

javafx-gradle / gradle-javafx-plugin / src / main / groovy / org / bitbucket / shemnon / javafxplugin / tasks / JavaFXDeployTask.groovy

shemnon 664e698 

























shemnon 3887373 
shemnon 664e698 
shemnon 32faa3d 
shemnon 22190dc 
shemnon 32faa3d 
shemnon 3ef71ca 
shemnon 22190dc 

shemnon 92164fe 
shemnon 22190dc 


shemnon 664e698 

shemnon 22190dc 
shemnon 816be1a 
shemnon 9a4fdf2 
shemnon 664e698 
shemnon 0e3d57d 
shemnon 22190dc 


shemnon 664e698 

shemnon 3ef71ca 















shemnon 92164fe 
shemnon 3ef71ca 



shemnon 0e3d57d 
shemnon 3ef71ca 

shemnon a54f3fc 
shemnon 3ef71ca 

shemnon 0e3d57d 
shemnon 06ca0b3 
shemnon 3ef71ca 














shemnon 664e698 

shemnon 32faa3d 

shemnon 06ca0b3 
shemnon 32faa3d 














































shemnon 664e698 
shemnon 3ef71ca 
shemnon 32faa3d 

shemnon a54f3fc 
shemnon 32faa3d 









shemnon a54f3fc 
shemnon 32faa3d 








shemnon 9a4fdf2 

shemnon 92164fe 
shemnon 9a4fdf2 




shemnon 22190dc 
shemnon 9a4fdf2 



shemnon 92164fe 


shemnon 9a4fdf2 
shemnon 3ef71ca 






shemnon 32faa3d 
shemnon 3ef71ca 


















shemnon 22190dc 
shemnon 3ef71ca 
shemnon 32faa3d 
shemnon 3ef71ca 
shemnon 664e698 

shemnon 9a4fdf2 
shemnon 92164fe 
shemnon 9a4fdf2 

shemnon 0e3d57d 














shemnon 3f01602 
shemnon 0e3d57d 
shemnon 92164fe 
shemnon 0e3d57d 
shemnon 9a4fdf2 
shemnon 86dce18 






shemnon 3ef71ca 
shemnon a54f3fc 
shemnon 86dce18 




shemnon 816be1a 
shemnon 22190dc 

shemnon 816be1a 
shemnon 22190dc 
shemnon 3ef71ca 
shemnon 816be1a 
shemnon 51a5bb4 

shemnon 3ef71ca 
shemnon 664e698 
shemnon 22190dc 

shemnon 3ef71ca 
shemnon 22190dc 
shemnon 3ef71ca 

shemnon 599ee04 
shemnon 3ef71ca 
shemnon 22190dc 
shemnon 3ef71ca 



shemnon 92164fe 
shemnon 3ef71ca 
shemnon 92164fe 
shemnon 3f01602 

shemnon 3ef71ca 

shemnon 3f01602 

shemnon 3ef71ca 

shemnon 3f01602 

shemnon 3ef71ca 

shemnon 3f01602 

shemnon 3ef71ca 

shemnon 2939db5 
shemnon 3f01602 
shemnon 3ef71ca 

shemnon 2939db5 
shemnon 3ef71ca 







shemnon 22190dc 
shemnon 664e698 
shemnon 22190dc 


shemnon 0e3d57d 
shemnon 22190dc 
shemnon 3ef71ca 
shemnon 664e698 
shemnon 51a5bb4 
shemnon 22190dc 
shemnon 92164fe 
shemnon 22190dc 
shemnon 92164fe 
shemnon 3f01602 

shemnon 22190dc 




















shemnon 51a5bb4 
shemnon 22190dc 
shemnon a54f3fc 
shemnon 22190dc 
shemnon 92164fe 
shemnon 3f01602 

shemnon 22190dc 


shemnon 3f01602 
shemnon 22190dc 




shemnon 3f01602 
shemnon 22190dc 









shemnon de53eb0 

shemnon 22190dc 

shemnon 51a5bb4 

shemnon a54f3fc 
shemnon 92164fe 
shemnon 51a5bb4 
shemnon 92164fe 
shemnon 3f01602 

shemnon 51a5bb4 

shemnon 3f01602 




shemnon 51a5bb4 
shemnon 3f01602 
shemnon 51a5bb4 



shemnon 3f01602 

shemnon 51a5bb4 


shemnon 9a4fdf2 
shemnon 92164fe 







shemnon 70ae33b 
shemnon 92164fe 
shemnon 9a4fdf2 
shemnon 92164fe 







shemnon 3f01602 

shemnon 92164fe 
shemnon 9a4fdf2 
shemnon 664e698 
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright (c) 2012, Danno Ferrin
 *   All rights reserved.
 *
 *   Redistribution and use in source and binary forms, with or without
 *   modification, are permitted provided that the following conditions are met:
 *       * Redistributions of source code must retain the above copyright
 *         notice, this list of conditions and the following disclaimer.
 *       * Redistributions in binary form must reproduce the above copyright
 *         notice, this list of conditions and the following disclaimer in the
 *         documentation and/or other materials provided with the distribution.
 *       * Neither the name of Danno Ferrin nor the
 *         names of contributors may be used to endorse or promote products
 *         derived from this software without specific prior written permission.
 *
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 *   DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 *   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 *   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 *   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 *   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bitbucket.shemnon.javafxplugin.tasks

import com.sun.javafx.tools.packager.DeployParams
import com.sun.javafx.tools.packager.Log
import com.sun.javafx.tools.packager.PackagerLib
import com.sun.javafx.tools.packager.bundlers.Bundler
import net.sf.image4j.codec.bmp.BMPEncoder
import net.sf.image4j.codec.ico.ICOEncoder
import org.bitbucket.shemnon.javafxplugin.IconInfo
import org.gradle.api.file.FileCollection
import org.gradle.api.internal.ConventionTask
import org.gradle.api.tasks.InputDirectory
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.internal.os.OperatingSystem
import org.gradle.util.ConfigureUtil

import javax.imageio.ImageIO
import java.awt.geom.AffineTransform
import java.awt.image.BufferedImage

class JavaFXDeployTask extends ConventionTask {

    String packaging

    FileCollection antJavaFXJar

    String appID
    String appName

    boolean verbose = false

    int width = 1024
    int height = 768
    boolean embedJNLP = false
    String updateMode = "background"
    boolean offlineAllowed = true
    String codebase

    String mainClass
    List<String> jvmArgs = []
    Map<String, String> systemProperties = [:]
    List<String> arguments = []

    // deploy/info attributes
    String category
    String copyright
    String deployDescription
    String licenseType
    String vendor
    List<IconInfo> iconInfos = []
    String version

    // deploy/preferences attributes
    Boolean installSystemWide
    boolean menu
    boolean shortcut

    @InputFiles
    FileCollection inputFiles

    @InputDirectory
    File resourcesDir

    @OutputDirectory
    File distsDir

    @TaskAction
    processResources() {
        DeployParams deployParams = new DeployParams();

        deployParams.version = getVersion()

        // these deploy params are currently not set
        //java.lang.String preloader;
        //java.util.List<com.sun.javafx.tools.packager.Param> params;
        //java.util.List<com.sun.javafx.tools.packager.HtmlParam> htmlParams;
        //boolean embedCertificates;
        //boolean isExtension;
        //boolean isSwingApp;
        //boolean includeDT;
        //java.lang.String placeholder;
        //java.util.List<com.sun.javafx.tools.ant.Callback> callbacks;
        //java.util.List<com.sun.javafx.tools.packager.DeployParams.Template> templates;
        //java.lang.String jrePlatform;
        //java.lang.String fxPlatform;
        //java.lang.String fallbackApp;


        deployParams.width = getWidth()
        deployParams.height = getHeight()
        deployParams.setEmbeddedDimensions(getWidth() as String, getHeight() as String) // FIXME make a convention property
        deployParams.setOutdir(getDistsDir())
        deployParams.embedJNLP = getEmbedJNLP()
        deployParams.outfile = getAppName() //FIXME duplicate with app name
        switch (getPackaging().toLowerCase()) {
            case "false":
            case "none":
                deployParams.bundleType = Bundler.BundleType.NONE
                deployParams.targetFormat = null
                break;
            case "all":
            case "true":
                deployParams.bundleType = Bundler.BundleType.ALL
                deployParams.targetFormat = null
                break;
            case "image":
                deployParams.bundleType = Bundler.BundleType.IMAGE
                deployParams.targetFormat = null
                break;
            case "installer":
                deployParams.bundleType = Bundler.BundleType.INSTALLER
                deployParams.targetFormat = null
                break;
            default:
                // assume the packageing is for a specific type
                deployParams.bundleType = Bundler.BundleType.INSTALLER
                deployParams.targetFormat = getPackaging()

        }

        deployParams.verbose = getVerbose()

        deployParams.appId = getAppID()
        deployParams.appName = getAppName() // FIXME duplicate with title
        deployParams.applicationClass = getMainClass()

        getInputFiles() each { File f ->
            deployParams.addResource(f.parentFile, f);
        }

        deployParams.title = getAppName()
        deployParams.category = getCategory()
        deployParams.copyright = getCopyright()
        deployParams.description = getDeployDescription()
        deployParams.licenseType = getLicenseType()
        deployParams.vendor = getVendor()

        deployParams.systemWide = getInstallSystemWide()
        deployParams.needMenu = getMenu()
        deployParams.needShortcut = getShortcut()

        deployParams.allPermissions = true //FIXME hardcoded

        deployParams.updateMode = getUpdateMode()
        deployParams.offlineAllowed = getOfflineAllowed()
        for (IconInfo ii : getIconInfos()) {
            deployParams.addIcon(ii.href, ii.kind, ii.width, ii.height, ii.depth, ii.mode);
        }
        if (getCodebase() != null) {
            try {
                deployParams.codebase = getCodebase()
            } catch (MissingPropertyException ignored) {
                getLogger().error("JavaFXDeployTask.codebase is only available in JavaFX 8 or later, codebase setting is ignored")
            }
        }

        getJvmArgs().each { deployParams.addJvmArg(it) }
        getSystemProperties().each {k, v -> deployParams.addJvmProperty(k, v)}
        deployParams.arguments = getArguments()

        File packageResourcesOutput = project.sourceSets['package'].output.resourcesDir
        processIcons(packageResourcesOutput)

        // hack
        deployParams.class.classLoader.addURL(packageResourcesOutput.parentFile.toURI().toURL())


        PackagerLib packager = new PackagerLib();
        Log.setLogger(new Log.Logger(getVerbose()) {
            @Override
            void info(String msg) {
                getLogger().info(msg)
            }

            @Override
            void verbose(String msg) {
                if (getVerbose()) {
                    info(msg)
                } else {
                    debug(msg)
                }
            }

            @Override
            void debug(String msg) {
                getLogger().debug(msg)
            }
        } as Log.Logger)

        packager.generateDeploymentPackages(deployParams)
        Log.setLogger(null)
    }

    def icon(Closure closure) {
        getIconInfos().add(new IconInfo(closure))
    }

    def icons(Closure closure) {
        Map m = [:]
        ConfigureUtil.configure(closure, m)
        m.each {k, v ->
            if (v instanceof List) {
                v.each {
                    addIcon(k, it)
                }
            } else {
                addIcon(k, v)
            }
        }
    }

    protected void addIcon(String kind, String href) {
        IconInfo ii = new IconInfo(href)
        ii.kind = kind
        getIconInfos().add(ii)
    }

    protected void loadConventialIcons(String kind) {
        // this convention is non-configurable, hence it gets hard coded
        project.fileTree('src/deploy/package', { include "$kind*.png"}).visit { fileDetails ->
            addIcon(kind, fileDetails.path)
        }
    }

    protected void processIcons(File destination) {
        if (getIconInfos().isEmpty()) {
            // if nothing is configured, use the convention
            loadConventialIcons('shortcut')
            loadConventialIcons('volume')
            loadConventialIcons('setup')
        }
        if (OperatingSystem.current().isMacOsX()) {
            processMacOSXIcons(destination);
        }
        if (OperatingSystem.current().isWindows()) {
            processWindowsIcons(destination);
        }
        if (OperatingSystem.current().isLinux()) {
            processLinuxIcons(destination)
        }
    }

    protected void processMacOSXIcons(File destination) {
        processMacOSXIcns('shortcut',
                new File(destination, "macosx/${project.javafx.appName}.icns"))
        processMacOSXIcns('volume',
                new File(destination, "macosx/${project.javafx.appName}-volume.icns"))
    }

    def macIcnsSizes = [16,32,128,256,512]
    protected void processMacOSXIcns(String kind, File iconLocation) {
        // get explicit
        def dest = "$project.buildDir/icons/${kind}.iconset"
        project.mkdir(dest)
        boolean createIcon = false
        for (IconInfo ii : getIconInfos()) {
            if (kind == ii.kind) {
                BufferedImage icon = getImage(ii)
                if (icon == null) {
                    logger.error("Icon $ii.href for $ii.kind rejected from MacOSX bundling because $ii.href does not exist or it is not an image.")
                    continue;
                }
                if (ii.width != ii.height) {
                    logger.info("Icon $ii.href for $ii.kind rejected from MacOSX bundling because it is not square: $ii.width x $ii.height.")
                    continue;
                }
                if (ii.scale != 1 && ii.scale != 2) {
                    logger.info("Icon $ii.href for $ii.kind rejected from MacOSX bundling because it has an invalid scale.")
                    continue;
                }
                if (!macIcnsSizes.contains(ii.width)) {
                    logger.info("Icon $ii.href for $ii.kind rejected from MacOSX bundling because it is an unsupported dimension.  $macIcnsSizes dimensions are supported.")
                    continue;
                }

                ant.copy(file: ii.file, toFile: "$dest/icon_${ii.width}x${ii.height}${ii.scale == 2 ? '@2x': ''}.png")
                createIcon = true
            }

        }
        if (createIcon) {
            project.exec {
                executable 'iconutil'
                args ('--convert', 'icns', dest)
            }
            ant.copy(file: "$project.buildDir/icons/${kind}.icns", toFile: iconLocation)
        }
    }

    protected void processWindowsIcons(File destination) {
        processWindowsIco('shortcut',
                new File(destination, "windows/${project.javafx.appName}.ico"))
        processWidnowsBMP('setup',
                new File(destination, "windows/${project.javafx.appName}-setup-icon.bmp"))
    }

    protected void processWidnowsBMP(String kind, File destination) {
        boolean processed = false
        for (IconInfo ii : getIconInfos()) {
            if (kind == ii.kind) {
                BufferedImage icon = getImage(ii)
                if (icon == null) {
                    logger.error("Icon $ii.href for $ii.kind rejected from Windows bundling because $ii.href does not exist or it is not an image.")
                    continue;
                }
                if (processed) {
                    logger.info("Icon $ii.href for $ii.kind rejected from Windows bundling because only one icon can be used.")
                    continue;
                }

                double scale = Math.min(Math.min(55.0 / icon.width, 58.0 / icon.height), 1.0)

                BufferedImage bi = new BufferedImage((int)icon.width*scale, (int)icon.height*scale, BufferedImage.TYPE_INT_ARGB)
                def g = bi.graphics
                def t = new AffineTransform()
                t.scale(scale, scale)
                g.transform = t
                g.drawImage(icon, 0, 0, null)
                BMPEncoder.write(bi, destination)
                processed = true
            }
        }
    }

    protected void processWindowsIco(String kind, File destination) {
        Map<Integer, BufferedImage> images = new TreeMap<Integer, BufferedImage>()
        for (IconInfo ii : getIconInfos()) {
            if (kind == ii.kind) {
                BufferedImage icon = getImage(ii)
                if (icon == null) {
                    logger.error("Icon $ii.href for $ii.kind rejected from Windows bundling because $ii.href does not exist or it is not an image.")
                    continue;
                }
                if (ii.scale != 1) {
                    logger.info("Icon $ii.href for $ii.kind rejected from Widnows bundling because it has a scale other than '1'.")
                    continue;
                }


                if (icon.width != icon.height) {
                    logger.info("Icon $ii.href for $ii.kind rejected from Windows bundling because it is not square: $icon.width x $icon.height.")
                    continue;
                }
                BufferedImage bi = new BufferedImage(icon.width, icon.height, BufferedImage.TYPE_INT_ARGB)
                bi.graphics.drawImage(icon, 0, 0, null)
                images.put(bi.width, bi)
            }

        }
        if (images) {
            destination.parentFile.mkdirs()
            List<BufferedImage> icons = (images.values() as List).reverse()
            ICOEncoder.write(icons, destination)
        }
    }

    protected void processLinuxIcons(File destination) {
        IconInfo largestIcon = null
        for (IconInfo ii : getIconInfos()) {
            if ('shortcut' == ii.kind) {
                BufferedImage icon = getImage(ii)
                if (icon == null) {
                    logger.error("Icon $ii.href for $ii.kind rejected from Linux bundling because $ii.href does not exist or it is not an image.")
                    continue;
                }
                if (largestIcon?.width < ii.width) {
                    if (largestIcon != null) {
                        logger.info("Icon $largestIcon.href for $largestIcon.kind rejected from Linux bundling because it is not the largest icon.")
                    }
                    largestIcon = ii
                } else {
                    logger.info("Icon $ii.href for $ii.kind rejected from Linux bundling because it is not the largest icon.")
                }
            }
        }

        if (largestIcon) {
             ant.copy(file: largestIcon.file, toFile: new File(destination, "linux/${project.javafx.appName.replaceAll('\\s', '')}.png"))
        }

    }

    BufferedImage getImage(IconInfo ii) {
        if (ii._image == null) {
            ii.file = getProject().file(ii.href)
            if (!ii.file.file) {
                // try to resolve relative to output
                ii.file = new File(getResourcesDir(), ii.href)
            }
            if (!ii.file.file) return

            ii._image = ImageIO.read(ii.file)

            if (ii.href.contains('@2x')) {
                ii.width = ii._image.width / 2
                ii.height = ii._image.height / 2
                ii.scale = 2
            } else {
                ii.width = ii._image.width
                ii.height = ii._image.height
                ii.scale = 1
            }
        }
        return ii._image
    }
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.