Skip to content

Commit a191e40

Browse files
authored
Merge pull request #109 from andrewmclachlanadi/bugfix/installer-iapplication
Provide a CMSIS-Pack command line installer IApplication
2 parents 12c7d79 + da9fe49 commit a191e40

4 files changed

Lines changed: 202 additions & 0 deletions

File tree

com.arm.cmsis.pack.installer/plugin.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,17 @@
1111
name="Default CMSIS-Pack installer ">
1212
</PackInstaller>
1313
</extension>
14+
<extension
15+
id="com.arm.cmsis.pack.installer"
16+
name="CMSIS-Pack command line installer"
17+
point="org.eclipse.core.runtime.applications">
18+
<application
19+
cardinality="singleton-global"
20+
thread="main"
21+
visible="true">
22+
<run
23+
class="com.arm.cmsis.pack.installer.ClPackInstaller">
24+
</run>
25+
</application>
26+
</extension>
1427
</plugin>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2023 Analog Devices, Inc. and others
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* Analog Devices - command line pack installer application
10+
*******************************************************************************/
11+
12+
package com.arm.cmsis.pack.installer;
13+
14+
import org.eclipse.core.runtime.NullProgressMonitor;
15+
import org.eclipse.equinox.app.IApplication;
16+
import org.eclipse.equinox.app.IApplicationContext;
17+
import org.eclipse.osgi.util.NLS;
18+
import org.eclipse.swt.widgets.Display;
19+
20+
import com.arm.cmsis.pack.CpPlugIn;
21+
import com.arm.cmsis.pack.ICpPackInstaller;
22+
import com.arm.cmsis.pack.data.ICpPack;
23+
import com.arm.cmsis.pack.data.ICpPackCollection;
24+
import com.arm.cmsis.pack.events.IRteEventListener;
25+
import com.arm.cmsis.pack.events.RteEvent;
26+
27+
/**
28+
* Command line pack installer
29+
*
30+
*/
31+
public class ClPackInstaller implements IApplication, IRteEventListener {
32+
33+
/** true if using the command line installer */
34+
private static boolean isHeadless = false;
35+
36+
/** return code for errors */
37+
private static final int EXIT_ERROR = 0x1;
38+
39+
/** true if an error has occurred while using the installer */
40+
private boolean errorOccurred = false;
41+
42+
/**
43+
* {@inheritDoc}
44+
*/
45+
@Override
46+
public Object start(IApplicationContext context) throws Exception {
47+
// suppress error dialogs
48+
System.setProperty(IApplicationContext.EXIT_DATA_PROPERTY, "");
49+
isHeadless = true;
50+
CpPlugIn.getDefault().addListener(this);
51+
52+
ICpPackInstaller packInstaller = CpPlugIn.getPackManager().getPackInstaller();
53+
if(packInstaller == null)
54+
return -1;
55+
56+
boolean showUsage = false;
57+
String[] args = (String[]) context.getArguments().get(IApplicationContext.APPLICATION_ARGS);
58+
if (args != null) {
59+
for (int i=0; i<args.length; i++) {
60+
String arg = args[i];
61+
if (arg.equals("-importPack")) {
62+
if (i+1 < args.length) {
63+
String packFile = args[i+1];
64+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_INFO, NLS.bind(Messages.CpPackInstaller_ImportingPack, packFile));
65+
packInstaller.importPack(packFile);
66+
i++;
67+
}
68+
else {
69+
showUsage = true;
70+
}
71+
}
72+
else if (arg.equals("-installPack")) {
73+
if (i+1 < args.length) {
74+
String packId = args[i+1];
75+
// retrieve the packs from the web so we can install one of them
76+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_INFO, Messages.ClPackInstaller_RetrievingPacks);
77+
packInstaller.updatePacks(new NullProgressMonitor());
78+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_INFO, NLS.bind(Messages.CpPackInstaller_InstallingPack, packId));
79+
packInstaller.installPack(packId);
80+
i++;
81+
}
82+
else {
83+
showUsage = true;
84+
}
85+
}
86+
else if (arg.equals("-deletePack")) {
87+
if (i+1 < args.length) {
88+
String packId = args[i+1];
89+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_INFO, NLS.bind(Messages.CpPackInstaller_DeletingPack, packId));
90+
ICpPackCollection packs = CpPlugIn.getPackManager().getInstalledPacks();
91+
ICpPack pack = null;
92+
if (packs != null) {
93+
pack = packs.getPack(packId);
94+
if (pack != null) {
95+
packInstaller.removePack(pack, true);
96+
}
97+
}
98+
if (packs == null || pack == null) {
99+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_ERROR, NLS.bind(Messages.ClPackInstaller_FailToDeletePack, packId));
100+
}
101+
i++;
102+
}
103+
else {
104+
showUsage = true;
105+
}
106+
}
107+
else if (arg.equals("-help")) {
108+
showUsage = true;
109+
break;
110+
}
111+
else {
112+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_ERROR, NLS.bind(Messages.ClPackInstaller_InvalidArg, arg));
113+
showUsage = true;
114+
break;
115+
}
116+
}
117+
118+
if (showUsage) {
119+
printUsage();
120+
}
121+
}
122+
123+
// wait for all jobs to finish
124+
while (packInstaller.isBusy()) {
125+
// flush the display to ensure all dialogs are shown
126+
if (!Display.getDefault().readAndDispatch()) {
127+
Thread.sleep(100);
128+
}
129+
}
130+
131+
if (errorOccurred) {
132+
return EXIT_ERROR;
133+
}
134+
135+
return EXIT_OK;
136+
}
137+
138+
/**
139+
* {@inheritDoc}
140+
*/
141+
@Override
142+
public void stop() {
143+
CpPlugIn.getDefault().removeListener(this);
144+
}
145+
146+
/**
147+
* Print the usage to the console
148+
*/
149+
public static void printUsage() {
150+
CpPlugIn.getDefault().emitRteEvent(RteEvent.PRINT_INFO, Messages.ClPackInstaller_Usage);
151+
}
152+
153+
/**
154+
* Returns true if using the command line installer (no UI)
155+
*
156+
* @return true if using the command line installer, otherwise false
157+
*/
158+
public static boolean isHeadless() {
159+
return isHeadless;
160+
}
161+
162+
/**
163+
* {@inheritDoc}
164+
*/
165+
@Override
166+
public void handle(RteEvent event) {
167+
switch(event.getTopic()) {
168+
case RteEvent.PRINT_OUTPUT:
169+
case RteEvent.PRINT_INFO:
170+
case RteEvent.PRINT_WARNING:
171+
System.out.println((String)event.getData());
172+
break;
173+
case RteEvent.PRINT_ERROR:
174+
errorOccurred = true;
175+
System.out.println((String)event.getData());
176+
break;
177+
}
178+
}
179+
}

com.arm.cmsis.pack.installer/src/com/arm/cmsis/pack/installer/Messages.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*
88
* Contributors:
99
* ARM Ltd and ARM Germany GmbH - Initial API and implementation
10+
* Analog Devices, Inc - Extension and implementation
1011
*******************************************************************************/
1112

1213
package com.arm.cmsis.pack.installer;
@@ -97,6 +98,11 @@ public class Messages extends NLS {
9798

9899
public static String PackInstallerUtils_PleaseAgreeLicenseAgreement;
99100

101+
public static String ClPackInstaller_FailToDeletePack;
102+
public static String ClPackInstaller_InvalidArg;
103+
public static String ClPackInstaller_RetrievingPacks;
104+
public static String ClPackInstaller_Usage;
105+
100106
static {
101107
// initialize resource bundle
102108
NLS.initializeMessages(BUNDLE_NAME, Messages.class);

com.arm.cmsis.pack.installer/src/com/arm/cmsis/pack/installer/messages.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
ClPackInstaller_FailToDeletePack=Failed to delete pack {0}. Pack not found.
2+
ClPackInstaller_InvalidArg=Invalid arg ''{0}''.
3+
ClPackInstaller_RetrievingPacks=Retrieving packs from the web.
4+
ClPackInstaller_Usage=Usage:\n -help : Print this message.\n -importPack <.pack file> : Import a local Software Pack file. You must accept the User License Agreement if the pack contains a license.\n -installPack <pack name> : Install a Software Pack file from the web. You must accept the User License Agreement if the pack contains a license.\n -deletePack <pack name> : Delete an imported or installed Software Pack file.
15
CpPackJob_CancelledByUser=Cancelled by user
26
CpPackImportFolderJob_ContainMorePdscFile=\ contains more than 1 pdsc file.
37
CpPackImportFolderJob_ImportingPack=Importing pack {0}

0 commit comments

Comments
 (0)