Skip to content

Commit

Permalink
fix: update core config file and handle read errors (#564)
Browse files Browse the repository at this point in the history
* fix(core): update core config file and handle read errors

The core config file containing environment variables is now build/dev.properties. Use that file instead of config.blt. Additionally handle any errors in parsing and reading by logging to console and not attempting to write to the file.

---------

Co-authored-by: Mike Senn <msenn@salesforce.com>
  • Loading branch information
randi274 and Mike Senn authored Apr 14, 2023
1 parent f86fec0 commit a166ac1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
23 changes: 5 additions & 18 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
let you know if there is already an effort in progress.
1. Fork this repository.
1. The [README](README.md) has details on how to set up your environment.
1. Create a _topic_ branch in your fork based on the correct branch (usually the **develop** branch, see [Branches section](#branches) below). Note, this step is recommended but technically not required if contributing using a fork.
1. Create a _topic_ branch in your fork based on the correct branch (usually the **main** branch, see [Branches section](#branches) below). Note, this step is recommended but technically not required if contributing using a fork.
1. Edit the code in your fork.
1. Sign CLA (see [CLA](#cla) below)
1. Send us a pull request when you are done. We'll review your code, suggest any
Expand All @@ -26,10 +26,10 @@ Agreement. You can do so by going to https://cla.salesforce.com/sign-cla.

## Branches

- We work in `develop`.
- We work in `main`.
- Our released (aka. _production_) branch is `main`.
- Our work happens in _topic_ branches (feature and/or bug-fix).
- feature as well as bug-fix branches are based on `develop`
- feature as well as bug-fix branches are based on `main`
- branches _should_ be kept up-to-date using `rebase`
- see below for further merge instructions

Expand All @@ -41,25 +41,12 @@ Agreement. You can do so by going to https://cla.salesforce.com/sign-cla.

- _Topic_ branches are:

1. based on `develop` and will be
1. squash-merged into `develop`.
1. based on `main` and will be
1. squash-merged into `main`.

- Hot-fix branches are an exception.
- Instead we aim for faster cycles and a generally stable `develop` branch.

### Merging `develop` into `main`

- When a development cycle finishes, the content of the `develop` branch will become the `main` branch

```
$ git checkout main
$ git reset --hard develop
$
$ # Using a custom commit message for the merge below
$ git merge -m 'Merge -s our (where _ours_ is develop) releasing stream x.y.z.' -s ours origin/main
$ git push origin main
```

## Pull Requests

- Develop features and bug fixes in _topic_ branches.
Expand Down
14 changes: 11 additions & 3 deletions packages/lightning-lsp-common/src/__tests__/context.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ import {
CORE_MULTI_ROOT,
} from './test-utils';

beforeAll(() => {
// make sure test runner config doesn't overlap with test workspace
delete process.env.P4PORT;
delete process.env.P4CLIENT;
delete process.env.P4USER;
});

it('WorkspaceContext', async () => {
let context = new WorkspaceContext('test-workspaces/sfdx-workspace');
expect(context.type).toBe(WorkspaceType.SFDX);
Expand Down Expand Up @@ -253,16 +260,17 @@ it('configureCoreProject()', async () => {
const settingsPath = CORE_PROJECT_ROOT + '/.vscode/settings.json';

// make sure no generated files are there from previous runs
fs.removeSync(jsconfigPath);
fs.removeSync(typingsPath);
await fs.remove(jsconfigPath);
await fs.remove(typingsPath);
await fs.remove(settingsPath);

// configure and verify typings/jsconfig after configuration:
await context.configureProject();

verifyJsconfigCore(jsconfigPath);
verifyTypingsCore();

const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf8'));
const settings = JSON.parse(await fs.readFile(settingsPath, 'utf8'));
verifyCoreSettings(settings);
});

Expand Down
40 changes: 26 additions & 14 deletions packages/lightning-lsp-common/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,10 @@ export class WorkspaceContext {
}

private async updateCoreSettings(): Promise<void> {
const configBlt = await this.readConfigBlt();
const configBlt = await this.readConfig();
const variableMap = {
eslint_node_path: await findCoreESLint(),
p4_port: configBlt['p4.port'],
p4_client: configBlt['p4.client'],
p4_user: configBlt['p4.user'],
...configBlt,
};
const templateString = await fs.readFile(utils.getCoreResource('settings-core.json'), 'utf8');
const templateContent = this.processTemplate(templateString, variableMap);
Expand All @@ -464,26 +462,40 @@ export class WorkspaceContext {
}

private async updateCoreCodeWorkspace(): Promise<void> {
const configBlt = await this.readConfigBlt();
const configBlt = await this.readConfig();
const variableMap = {
eslint_node_path: await findCoreESLint(),
p4_port: configBlt['p4.port'],
p4_client: configBlt['p4.client'],
p4_user: configBlt['p4.user'],
...configBlt,
};
const templateString = await fs.readFile(utils.getCoreResource('core.code-workspace.json'), 'utf8');
const templateContent = this.processTemplate(templateString, variableMap);
this.updateConfigFile('core.code-workspace', templateContent);
}

private async readConfigBlt(): Promise<any> {
const isMain = this.workspaceRoots[0].indexOf(path.join('main', 'core')) !== -1;
let relativeBltDir = isMain ? path.join('..', '..', '..') : path.join('..', '..', '..', '..');
// As of 04/2023, core users define Perforce variables in env vars.
// Fallback to build/user.properties, which some users have configured.
private async readConfig(): Promise<{ p4_port?: string; p4_client?: string; p4_user?: string }> {
let userProperties;
if (this.type === WorkspaceType.CORE_PARTIAL) {
relativeBltDir = path.join(relativeBltDir, '..');
// most common because this is the workspace corecli generates
userProperties = path.join(this.workspaceRoots[0], '..', 'build', 'user.properties');
} else if (this.type === WorkspaceType.CORE_ALL) {
userProperties = path.join(this.workspaceRoots[0], 'build', 'user.properties');
}
const configBltContent = await fs.readFile(path.join(this.workspaceRoots[0], relativeBltDir, 'config.blt'), 'utf8');
return parse(configBltContent);

let properties: any = {};
try {
const userPropertiesContent = await fs.readFile(userProperties, 'utf8');
properties = parse(userPropertiesContent);
} catch (error) {
console.warn(`Error reading core config. Continuing, but may be missing some config. ${error}`);
}

return {
p4_port: process.env.P4PORT || properties['p4.port'],
p4_client: process.env.P4CLIENT || properties['p4.client'],
p4_user: process.env.P4USER || properties['p4.user'],
};
}

private processTemplate(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
p4.user = username
p4.client = username-localhost-blt
p4.port = ssl:host:port
eclipse.default.jdk = path_to_java_home
eclipse.default.jdk = path_to_java_homes

0 comments on commit a166ac1

Please sign in to comment.