|
| 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 | +} |
0 commit comments