Skip to content

Commit

Permalink
feat: Issue #101 and #119 Added optional make flags to the configurat…
Browse files Browse the repository at this point in the history
…ion.

fix: Issue Incorrect YAML formatting results in silent delete and rebuild of YAML file
chore: updated Changelog
chore: bumped version number
  • Loading branch information
jortbmd committed Nov 14, 2022
1 parent 2596037 commit 76328ed
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 48 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
### Added
- Add support for unit testing.
- Add support for more in depth search for SVD files.

## [3.2.3] - 2022-11-14
### Addded
- Issue #101 & #119: Added make flags to the configuration file. This allows to add the --silent flag to the build and to prevent output mixing on parallel compilation.
### Fixed
- Issue #116: Fixed issue where the STM32 configuration file would silently delete if there was something wrong while parsing the yaml file.
## [3.2.2] - 2022-06-29
### Fixed
- Fixed Issue #96: Fixed issue where debugging for C++ gave no source file error due to missing debugging flags.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "stm32-for-vscode",
"displayName": "stm32-for-vscode",
"description": "An extension for: setting up, compiling, uploading and debugging STM32 applications",
"version": "3.2.2",
"version": "3.2.4",
"engines": {
"vscode": "^1.44.0"
},
Expand Down
3 changes: 2 additions & 1 deletion src/BuildTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ export default async function buildSTM(options?: { flash?: boolean; cleanBuild?:
currentWorkspaceFolder = fsPathToPosix(workspace.workspaceFolders[0].uri.fsPath);

info = await getInfo(currentWorkspaceFolder);
const makeArguments = `-j16 -f ${makefileName}`;
const makeFlags = info.makeFlags.length > 0 ? ` ${info.makeFlags.join(' ')}` : '';
const makeArguments = `-j16${makeFlags} -f ${makefileName}`;
if (cleanBuild) {
try {
await executeTask(
Expand Down
65 changes: 41 additions & 24 deletions src/configuration/stm32Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import * as _ from 'lodash';
import * as path from 'path';
import * as vscode from 'vscode';

import { EXTENSION_CONFIG_NAME } from '../Definitions';
import { ExtensionConfiguration, ExtensionConfigurationInterface } from '../types/MakeInfo';

import { EXTENSION_CONFIG_NAME } from '../Definitions';

const DEFAULT_SOURCES = ['Src/**', 'Core/Src/**', 'Core/Lib/**'];
const DEFAULT_INCLUDES = ['Inc/**', 'Core/Inc/**', 'Core/Lib/**'].concat(DEFAULT_SOURCES);

Expand Down Expand Up @@ -112,6 +113,11 @@ customMakefileRules:
# - command: sayhello
# rule: echo "hello"
# dependsOn: $(BUILD_DIR)/$(TARGET).elf # can be left out
# Additional flags which will be used when invoking the make command
makeFlags:
# - -O # use this option when the output of make is mixed up only works for make version 4.0 and upwards
# - --silent # use this option to silence the output of the build
`
);
}
Expand Down Expand Up @@ -141,8 +147,6 @@ export async function writeDefaultConfigFile(config: ExtensionConfiguration): Pr
return configFileWithAddedDefaults;
}

const PARSE_YAML_ERROR_MESSAGE = 'Could not parse yaml configuration';

/**
* read the stm32-for-vscode.config.yaml from disk
* @returns return the stm32-for-vscode.config.yaml in string format
Expand All @@ -160,12 +164,25 @@ export async function getConfigFileFromWorkspace(): Promise<string> {
* reads and parses the stm32-for-vscode.config.yaml file
* @returns The configuration of the current project
*/
export async function readConfigFile(): Promise<ExtensionConfiguration> {
const configuration = new ExtensionConfiguration();
export async function readConfigFile(): Promise<string> {
try {
const file = await getConfigFileFromWorkspace();
const yamlConfig: ExtensionConfigurationInterface = YAML.parse(file);
if (!yamlConfig) { return Promise.reject(new Error('Could not parse yaml configuration')); }
return file;
} catch (err) {
throw err;
}
}

/**
* Parses the STM32 for VSCode configuration file.
* @param configurationFile The configuration file in string format which will be parsed
* @returns ExtensionConfiguration or throws an error when it cannot parse it.
*/
export function parseConfigfile(configurationFile: string): ExtensionConfiguration {
const configuration = new ExtensionConfiguration();
try {
const yamlConfig: ExtensionConfigurationInterface = YAML.parse(configurationFile);
if (!yamlConfig) { new Error('Could not parse yaml configuration');}
_.forEach(yamlConfig, (entry, key) => {
if (_.has(yamlConfig, key) && _.get(yamlConfig, key) !== null && _.get(yamlConfig, key)?.[0] !== null) {
_.set(configuration, key, entry);
Expand All @@ -187,27 +204,27 @@ export async function readConfigFile(): Promise<ExtensionConfiguration> {
export async function readOrCreateConfigFile(config: ExtensionConfiguration): Promise<ExtensionConfiguration> {
const workspaceFolderUri = Helpers.getWorkspaceUri();
if (!workspaceFolderUri) { return config; }
let configFile: string = '';
try {
const configFile = await readConfigFile();
return configFile;
} catch (err) {
// no config file present
const { message } = err as Error;
if (message === PARSE_YAML_ERROR_MESSAGE) {
vscode.window.showErrorMessage(
`Could not parse: ${EXTENSION_CONFIG_NAME}, please check for Errors or delete it so it can be regenerated`
);
return config; // returns the standard configuration
configFile = await readConfigFile();
} catch (error) {
// if no config file is present. Create a new one.
try {
const newConfig = await writeDefaultConfigFile(config);
return newConfig;
} catch (error) {
vscode.window.showErrorMessage(`Something went wrong with writing the configuration file: ${error}`);
throw error;
}
}

// if no config file is present. Create a new one.
try {
const newConfig = await writeDefaultConfigFile(config);
return newConfig;
} catch (err) {
vscode.window.showErrorMessage(`Something went wrong with writing the configuration file: ${err}`);
const configuration = parseConfigfile(configFile);
return configuration;
} catch (error) {
vscode.window.showErrorMessage(
`Could not parse: ${EXTENSION_CONFIG_NAME}, it generated the following error messages: ${error}`
);
throw error;
}
return config;

}
1 change: 1 addition & 0 deletions src/getInfo/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export async function getInfo(location: string): Promise<MakeInfo> {
STM32MakeInfo.ldscript = projectConfiguration.ldscript;
STM32MakeInfo.mcu = cubeMakefileInfo.mcu;
STM32MakeInfo.targetMCU = projectConfiguration.targetMCU;
STM32MakeInfo.makeFlags = projectConfiguration.makeFlags;
const buildTools = getBuildToolsFromSettings();
STM32MakeInfo.tools = {
...STM32MakeInfo.tools,
Expand Down
1 change: 1 addition & 0 deletions src/test/fixtures/makeInfoFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function newMakeInfo(info: Partial<MakeInfo>): MakeInfo {
ldFlags: [],
cxxFlags: [],
customMakefileRules: undefined,
makeFlags: [],
};
return _.assign(standard, info);
}
1 change: 1 addition & 0 deletions src/test/fixtures/testSTMCubeMakefile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export const testMakefileInfo: MakeInfo = {
cxxFlags: [],
optimization: 'Og',
customMakefileRules: undefined,
makeFlags: [],
};

export const stm32ForVSCodeResult = `##########################################################################################################################
Expand Down
5 changes: 3 additions & 2 deletions src/test/integration/cxxBuildAndImport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
waitForWorkspaceFoldersChange
} from '../helpers';
import { afterEach, beforeEach, suite, test } from 'mocha';
import { readConfigFile, writeConfigFile } from '../../configuration/stm32Config';
import { parseConfigfile, readConfigFile, writeConfigFile } from '../../configuration/stm32Config';

import buildSTM from '../../BuildTask';
import importAndSetupCubeIDEProject from '../../import';
Expand All @@ -27,7 +27,8 @@ suite('import and convert to C++ test', () => {
await importAndSetupCubeIDEProject();

// change the config to c++
const projectConfiguration = await readConfigFile();
const configurationFile = await readConfigFile();
const projectConfiguration = await parseConfigfile(configurationFile);
projectConfiguration.language = 'C++';
await writeConfigFile(projectConfiguration);
await buildSTM();
Expand Down
15 changes: 0 additions & 15 deletions src/test/unit/Info.test.ts

This file was deleted.

8 changes: 5 additions & 3 deletions src/test/unit/configuration/stm32Config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import * as STM32Config from '../../../configuration/stm32Config';
import * as Sinon from 'sinon';
import * as vscode from 'vscode';

import { suite, test, beforeEach } from 'mocha';
import { beforeEach, suite, test } from 'mocha';

import { TextEncoder } from 'util';
import { configurationFixture } from '../../fixtures/extensionConfigurationFixture';
import { expect } from 'chai';
import { makeFSOverWritable } from '../../helpers/fsOverwriteFunctions';
import { TextEncoder } from 'util';

suite('STM32Config', () => {
beforeEach(() => {
makeFSOverWritable(vscode);
Expand All @@ -21,7 +22,8 @@ suite('STM32Config', () => {
Sinon.replace(vscode.workspace.fs, 'readFile', readFileFake);
Sinon.replace(Helpers, 'getWorkspaceUri', Sinon.fake.returns(vscode.Uri.file('')));
const configFile = await STM32Config.readConfigFile();
expect(configFile).to.deep.equal(configurationFixture);
const configuration = STM32Config.parseConfigfile(configFile);
expect(configuration).to.deep.equal(configurationFixture);
Sinon.restore();
});
});
7 changes: 6 additions & 1 deletion src/types/MakeInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Libraries implements LibrariesInterface {
public libraryDirectories: string[] = [];
}

// NOTE: this differs from the configuration in the shortinening of the DEFS names
// NOTE: this differs from the configuration in the shortening of the DEFS names
// This is maintained as this helps in parsing the makefile however should be noted
// when merging the two information sources.
export interface MakeInfoInterface extends BuildFilesInterface, TargetInfoInterface {
Expand All @@ -102,6 +102,7 @@ export interface MakeInfoInterface extends BuildFilesInterface, TargetInfoInterf
asDefs: string[];
tools: ToolChain;
customMakefileRules?: CustomMakefileRulesInterface[];
makeFlags?: string[];
}

export class ToolChain implements ToolChainInterface {
Expand All @@ -124,6 +125,7 @@ export interface ExtensionConfigurationInterface extends TargetInfoInterface, Co
sourceFiles: string[];
suppressMakefileWarning: boolean;
customMakefileRules?: CustomMakefileRulesInterface[];
makeFlags: string[];
}


Expand Down Expand Up @@ -169,6 +171,7 @@ export class ExtensionConfiguration implements ExtensionConfigurationInterface {
public libraryDirectories: string[] = [];
public suppressMakefileWarning = false;
public customMakefileRules: CustomMakefileRulesInterface[] | undefined = undefined;
public makeFlags: string[] = [];


public importRelevantInfoFromMakefile(makeInfo: MakeInfo): void {
Expand Down Expand Up @@ -208,6 +211,7 @@ export default class MakeInfo implements MakeInfoInterface {
public fpu = '';
public floatAbi = '';
public mcu = '';

public ldscript = '';
public targetMCU = '';
public language = 'C' as STM32Languages;
Expand All @@ -217,4 +221,5 @@ export default class MakeInfo implements MakeInfoInterface {
public ldFlags: string[] = [];
public cxxFlags: string[] = [];
public customMakefileRules: CustomMakefileRulesInterface[] | undefined = undefined;
public makeFlags: string[] = [];
}

0 comments on commit 76328ed

Please sign in to comment.