Skip to content

Commit

Permalink
Bundle JREs, and other OS compatibility fixes (#24)
Browse files Browse the repository at this point in the history
Several OS compatibility fixes:

- Bundle JREs with the CGSuite distribution, in order to avoid various issues due to JRE version conflicts.
- Use the correct user dir on Windows.
- Improved logic for instantiating a user folder and startup script.
  • Loading branch information
aaron-siegel authored Oct 1, 2021
1 parent f1b40d9 commit 2e03853
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ desktop-app/dist/
# CGSuite-related
cgsuite-kernel.log
lib/core/local/
desktop-app/local/
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ public BrowserTopComponent() {
try
{
// TODO Ensure this works.
/*
if (!USER_FOLDER.exists())
{
File defaultUserFolder = InstalledFileLocator.getDefault().locate("etc/default-userdir", "org.cgsuite", false);
copyFolder(defaultUserFolder, USER_FOLDER);
}
*/
if (!USER_FOLDER.exists()) {
USER_FOLDER.mkdir();
}
LocalFileSystem fs = new LocalFileSystem();
fs.setRootDirectory(USER_FOLDER);
fs.setReadOnly(false);
Expand Down Expand Up @@ -101,10 +106,10 @@ public BrowserTopComponent() {
if (System.getProperty("org.cgsuite.devbuild") != null)
{
// Add some convenience folders for developers
File defaultUserdir = new File(System.getProperty("org.cgsuite.devbuild"), "release/etc/default-userdir");
//File defaultUserdir = new File(System.getProperty("org.cgsuite.devbuild"), "release/etc/default-userdir");
jComboBox1.addItem(new RootFolder(FileUtil.toFileObject(DEV_LIB_FOLDER), "[dev] Core Library Source Folder"));
jComboBox1.addItem(new RootFolder(FileUtil.toFileObject(DEV_TEST_FOLDER), "[dev] Core Library Test Folder"));
jComboBox1.addItem(new RootFolder(FileUtil.toFileObject(defaultUserdir), "[dev] Default User Folder"));
//jComboBox1.addItem(new RootFolder(FileUtil.toFileObject(defaultUserdir), "[dev] Default User Folder"));
jComboBox1.addItem(new RootFolder(FileUtil.getConfigRoot(), "[dev] System Filesystem"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,6 @@ public void actionPerformed(ActionEvent e) {
}
};

static {
// Force HelpIndex to load, in order to reduce UI sluggishness later
HelpIndex.lookup("C");
}

public CgsuiteHelpTopComponent() {

initComponents();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ else if (":info".equals(text))
try
{
long startTime = System.nanoTime();
output = JavaConverters.seqAsJavaList(org.cgsuite.lang.System.evaluate(text, varMap));
output = JavaConverters.asJava(org.cgsuite.lang.System.evaluate(text, varMap));
long duration = System.nanoTime() - startTime;
log.info(String.format("Calculation finished in %d.%03d seconds.", duration / 1000000000L, (duration % 1000000000L) / 1000000L));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.cgsuite.ui.worksheet;

import java.io.File;
import javax.swing.filechooser.FileSystemView;
import org.cgsuite.help.HelpIndex;
import org.cgsuite.lang.CgscriptClasspath;
import scala.Symbol;
import scala.collection.mutable.AnyRefMap;

/**
*
* @author asiegel
*/
class WorksheetEnvironment {

final static AnyRefMap<Symbol,Object> WORKSPACE_VAR_MAP = new AnyRefMap<Symbol,Object>();

static void initialize() {

// Forcibly instantiate a CanonicalShortGame so that the interface will seem snappier
// once the user starts using it
new CalculationCapsule(WORKSPACE_VAR_MAP, "{1|1/2}").runAndWait();

File homeFolder = FileSystemView.getFileSystemView().getDefaultDirectory();
File userFolder = new File(homeFolder, "CGSuite");

if (!userFolder.exists()) {
userFolder.mkdir();
}
CgscriptClasspath.declareClasspathRoot(userFolder, true);

// Force HelpIndex to load, in order to reduce UI sluggishness later
// (This is just a convenient place for this to go that is guaranteed
// to load at startup)
HelpIndex.lookup("C");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@
import org.openide.util.TaskListener;
import org.openide.windows.TopComponent;
import org.openide.windows.WindowManager;
import scala.Symbol;
import scala.collection.mutable.AnyRefMap;


// TODO Check if there are unsaved files before starting a calculation
// TODO Make error output singly selectable
Expand All @@ -55,8 +52,6 @@
public class WorksheetPanel extends JPanel
implements Scrollable, KeyListener, TaskListener, DocumentListener, CommandListener
{
final static AnyRefMap<Symbol,Object> WORKSPACE_VAR_MAP = new AnyRefMap<Symbol,Object>();

private boolean deferredAdvance;

private InputPanel activeInputPanel;
Expand Down Expand Up @@ -99,12 +94,7 @@ private void initComponents() {

public void initialize()
{
// Forcibly instantiate a CanonicalShortGame so that the interface will seem snappier
// once the user starts using it
new CalculationCapsule(WORKSPACE_VAR_MAP, "{1|1/2}").runAndWait();
processCommand("startup();");
getBuffer();

getViewport().addComponentListener(new ComponentAdapter()
{
@Override
Expand Down Expand Up @@ -268,11 +258,11 @@ private synchronized void processCommand(InputPane source)
processCommand(source.getText());
}

private synchronized void processCommand(String command)
synchronized void processCommand(String command)
{
assert SwingUtilities.isEventDispatchThread();

CalculationCapsule capsule = new CalculationCapsule(WORKSPACE_VAR_MAP, command);
CalculationCapsule capsule = new CalculationCapsule(WorksheetEnvironment.WORKSPACE_VAR_MAP, command);
RequestProcessor.Task task = capsule.createTask();
task.addTaskListener(this);
task.schedule(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public final class WorksheetTopComponent extends TopComponent

private boolean started = false;

// WorksheetTopComponent loads no matter what, so we trigger initialization here.

static {
WorksheetEnvironment.initialize();
}

public WorksheetTopComponent()
{
initComponents();
Expand All @@ -45,8 +51,9 @@ public WorksheetTopComponent()
setToolTipText(NbBundle.getMessage(WorksheetTopComponent.class, "HINT_WorksheetTopComponent"));
setIcon(ImageUtilities.loadImage(ICON_PATH, true));
putClientProperty(TopComponent.PROP_CLOSING_DISABLED, Boolean.TRUE);

UiHarness$.MODULE$.setUiHarness(new WorksheetUiHarness());

UiHarness.setUiHarness(new WorksheetUiHarness());
worksheetPanel1.processCommand("startup();");
}

/** This method is called from within the constructor to
Expand Down Expand Up @@ -201,12 +208,12 @@ public void postOutput(List<Output> output)

class WorksheetUiHarness implements UiHarness {

private ExplorerService explorerService = Lookup.getDefault().lookup(ExplorerService.class);
private final ExplorerService explorerService = Lookup.getDefault().lookup(ExplorerService.class);

@Override
public void clearUiVars()
{
WorksheetPanel.WORKSPACE_VAR_MAP.clear();
WorksheetEnvironment.WORKSPACE_VAR_MAP.clear();
}

@Override
Expand All @@ -218,7 +225,7 @@ public Explorer createExplorer(Game g)
@Override
public void print(Object obj)
{
List<Output> output = JavaConverters.seqAsJavaList(org.cgsuite.lang.System.objectToOutput(obj));
List<Output> output = JavaConverters.asJava(org.cgsuite.lang.System.objectToOutput(obj));
postOutput(output);
}

Expand Down
2 changes: 1 addition & 1 deletion desktop-app/etc/create-dmg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ test -d "$AUX_PATH" || {
# Create the image
echo "Creating disk image..."
test -f "${DMG_TEMP_NAME}" && rm -f "${DMG_TEMP_NAME}"
hdiutil create -srcfolder "$SRC_FOLDER" -volname "${VOLUME_NAME}" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -size 300m "${DMG_TEMP_NAME}"
hdiutil create -srcfolder "$SRC_FOLDER" -volname "${VOLUME_NAME}" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -size 500m "${DMG_TEMP_NAME}"

# mount it
echo "Mounting disk image..."
Expand Down
3 changes: 3 additions & 0 deletions desktop-app/etc/macbundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ else
./make-alias.sh /Applications $2
fi

echo Copying JRE.
cp -r ../local/jre-bundles/macos/jre ../dist/macbundle/CGSuite.app/Contents/Resources/CGSuite

./create-dmg.sh \
--volname CGSuite \
--background dmg-background.png \
Expand Down
4 changes: 4 additions & 0 deletions desktop-app/nbproject/cgsuite-app.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
default_userdir="${DEFAULT_USERDIR_ROOT}/dev"
default_cachedir="${DEFAULT_CACHEDIR_ROOT}/dev"
default_options="--branding cgsuite"
jdkhome=jre
1 change: 1 addition & 0 deletions desktop-app/nbproject/project.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
app.conf=nbproject/cgsuite-app.conf
app.icon=branding/core/core.jar/org/netbeans/core/startup/frame48.gif
app.name=CGSuite
app.title=CGSuite
Expand Down
17 changes: 0 additions & 17 deletions desktop-app/release/etc/default-userdir/startup.cgs

This file was deleted.

9 changes: 6 additions & 3 deletions lib/core/src/main/scala/org/cgsuite/lang/CgscriptClass.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@ object CgscriptClass {
ord
}

logger debug "Declaring system classes."
logger debug "Declaring explicitly defined system classes."

SystemClassRegistry.allSystemClasses foreach { case (name, scalaClass) =>
declareSystemClass(name, Some(scalaClass))
}

logger debug "Declaring folders."
if (CgscriptClasspath.devBuildHome.isDefined)
logger debug "This is a CGSuite developer build."

CgscriptClasspath.declareFolders()
logger debug "Declaring system folder."

CgscriptClasspath.declareSystemClasspathRoot()

val Object = CgscriptPackage.lookupClassByName("Object").get
val Class = CgscriptPackage.lookupClassByName("Class").get
Expand Down
61 changes: 49 additions & 12 deletions lib/core/src/main/scala/org/cgsuite/lang/CgscriptClasspath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,29 @@ import scala.jdk.CollectionConverters._

object CgscriptClasspath {

val defaultStartupScript =
"""/*
| * This is your CGSuite startup file.
| *
| * Any commands that you include in this file will be executed whenever
| * you start CGSuite.
| *
| * You can re-run this script at any time without restarting CGSuite by
| * entering the following command on the Worksheet:
| *
| * startup();
| *
| */
|
|// Uncomment the following line to implement the shortcut "C(g)" for
|// the canonical form "g.CanonicalForm".
|
|// def C(g) := g.CanonicalForm;
|""".stripMargin

private[cgsuite] val devBuildHome = Option(java.lang.System.getProperty("org.cgsuite.devbuild")) map { File(_) }

private[cgsuite] val cgsuiteDir = java.lang.System.getProperty("user.home")/"CGSuite"
private[cgsuite] val classpathRoots = mutable.ArrayBuffer[File]()

private[cgsuite] val systemDir = {
devBuildHome match {
Expand All @@ -32,22 +52,35 @@ object CgscriptClasspath {
}
}

private[cgsuite] val classpath: Vector[File] = Vector(systemDir, cgsuiteDir)

private[cgsuite] val modifiedFiles = mutable.Set[ModifiedFile]()

private[cgsuite] def declareFolders(): Unit = {
logger debug "Declaring folders."
if (devBuildHome.isDefined)
logger debug "This is a CGSuite developer build."
logger debug s"System dir: $systemDir"
logger debug s"User dir: $cgsuiteDir"
classpath foreach declareFolder
def declareSystemClasspathRoot(): Unit = {
declareClasspathRoot(systemDir, ensureStartupScriptExists = false)
}

private[cgsuite] def declareFolder(folder: File): Unit = {
def declareClasspathRoot(folder: java.io.File, ensureStartupScriptExists: Boolean): Unit = {
declareClasspathRoot(folder.toScala, ensureStartupScriptExists)
}

def declareClasspathRoot(folder: File, ensureStartupScriptExists: Boolean): Unit = {
logger debug s"Declaring classpath root: $folder"
classpathRoots += folder
if (folder.fileSystem.provider.getScheme != "jar")
new Monitor(folder).start()
if (ensureStartupScriptExists) {
val startupScriptFile = folder / "startup.cgs"
try {
if (!startupScriptFile.exists) {
startupScriptFile overwrite defaultStartupScript
}
} catch {
case exc: Exception => logger.warn(s"Could not create startup script $startupScriptFile: ${exc.getMessage}", exc)
}
}
declareFolder(folder)
}

private[cgsuite] def declareFolder(folder: File): Unit = {
declareFolderR(CgscriptPackage.root, folder, folder)
}

Expand All @@ -57,7 +90,11 @@ object CgscriptClasspath {
if (file.isDirectory) {
declareFolderR(pkg.declareSubpackage(file.name stripSuffix "/"), root, file)
} else if (file.extension exists { _.toLowerCase == ".cgs" }) {
pkg.declareClass(Symbol(file.nameWithoutExtension), UrlClassDef(root, file.url), None)
val url = folder.fileSystem.provider.getScheme match {
case "jar" => getClass.getResource(file.toString)
case _ => file.url
}
pkg.declareClass(Symbol(file.nameWithoutExtension), UrlClassDef(root, url), None)
}
}
}
Expand Down

0 comments on commit 2e03853

Please sign in to comment.