Skip to content

Commit

Permalink
fix: remove generated files from examples index (#1076)
Browse files Browse the repository at this point in the history
* fix: remove generated files from examples index

* updated DocSource tests

* expanding ignore path list
  • Loading branch information
jkaster authored May 9, 2022
1 parent 9ed6ecd commit d75bce8
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 454 deletions.
478 changes: 45 additions & 433 deletions examplesIndex.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import React from 'react'
import { screen, waitFor } from '@testing-library/react'
import type { IDeclarationMine } from '@looker/sdk-codegen'
import { permaLink } from '@looker/sdk-codegen'
import { codeSearchLink } from '@looker/sdk-codegen'
import userEvent from '@testing-library/user-event'
import { api } from '../../test-data'
import { renderWithLode } from '../../test-utils'
Expand Down Expand Up @@ -82,7 +82,7 @@ describe('DocSource', () => {
renderWithLode(<DocSource type={type} />, examples, declarations)
const link = screen.getByRole('link')
const declaration = declarations.types.Query
const expected = permaLink(
const expected = codeSearchLink(
declarations.remoteOrigin,
declarations.commitHash,
declaration.sourceFile,
Expand All @@ -101,7 +101,7 @@ describe('DocSource', () => {
renderWithLode(<DocSource method={method} />, examples, declarations)
const link = screen.getByRole('link')
const declaration = declarations.methods['POST /login']
const expected = permaLink(
const expected = codeSearchLink(
declarations.remoteOrigin,
declarations.commitHash,
declaration.sourceFile,
Expand Down
5 changes: 3 additions & 2 deletions packages/api-explorer/src/components/DocSource/DocSource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import type { FC } from 'react'
import React from 'react'
import type { IMethod, IType } from '@looker/sdk-codegen'
import { findDeclaration } from '@looker/sdk-codegen'
import { codeSearchLink, findDeclaration } from '@looker/sdk-codegen'
import { Icon, Link, Tooltip } from '@looker/components'
import { IdeFileDocument } from '@looker/icons'
import { useSelector } from 'react-redux'
Expand All @@ -46,7 +46,8 @@ export const DocSource: FC<DocSourceProps> = ({ method, type }) => {
;({ declaration, link: sourceLink } = findDeclaration(
declarations,
method?.id,
type?.name
type?.name,
codeSearchLink
))
}

Expand Down
11 changes: 10 additions & 1 deletion packages/sdk-codegen-scripts/scripts/mineDeclarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
const indexName = `declarationsIndex.json`
let sourcePath = ''
let copyPath = ''
let originOverride = ''
if (total > 0) {
sourcePath = path.join(root, args[0])
if (total > 1) {
Expand All @@ -54,6 +55,9 @@ import {
if (settings.copy_path) {
copyPath = path.join(root, settings.copy_path)
}
if (settings.origin_override) {
originOverride = settings.origin_override
}
} catch (e) {
console.error(
'A source path is required. Specify it with "base_url" in the Miner section in looker.ini or pass it as an argument'
Expand All @@ -64,7 +68,12 @@ import {
const indexFile = path.join(root, indexName)
console.log(`Mining declarations from ${sourcePath} ...`)

const miner = new DeclarationMiner(sourcePath, rubyMethodProbe, rubyTypeProbe)
const miner = new DeclarationMiner(
sourcePath,
rubyMethodProbe,
rubyTypeProbe,
originOverride
)
const result = miner.execute()
fs.writeFileSync(indexFile, JSON.stringify(result, null, 2), {
encoding: 'utf-8',
Expand Down
24 changes: 19 additions & 5 deletions packages/sdk-codegen-scripts/src/declarationMiner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ const config = TestConfig()

/**
* This test suite requires a "Miner" section in the root's looker.ini with a
* base_url. Its value is the relative path to to the directory to be mined.
* base_url. Its value is the relative path to the directory to be mined.
* Tests are skipped if this configuration is not found.
*/
describe('Declaration miner', () => {
Expand All @@ -49,14 +49,16 @@ describe('Declaration miner', () => {
).readConfig()

const sourcePath = settings.base_url
const isConfigured = () => settings.base_url
const testIfConfigured = isConfigured() ? it : it.skip
const originOverride = settings.origin_override
const isConfigured = () => !!sourcePath

testIfConfigured('should mine files matching the probe settings', () => {
test('should mine files matching the probe settings', () => {
if (!isConfigured()) return
const miner = new DeclarationMiner(
sourcePath,
rubyMethodProbe,
rubyTypeProbe
rubyTypeProbe,
originOverride
)
const actual = miner.execute()
expect(actual.commitHash).toBeDefined()
Expand All @@ -71,4 +73,16 @@ describe('Declaration miner', () => {
expect(rubyTypeProbe.fileNamePattern.test(value.sourceFile)).toBe(true)
})
})

test('should retrieve remoteOrigin', () => {
if (!isConfigured()) return
const miner = new DeclarationMiner(
sourcePath,
rubyMethodProbe,
rubyTypeProbe,
originOverride
)
const actual = miner.remoteOrigin
expect(actual).not.toEqual('')
})
})
10 changes: 7 additions & 3 deletions packages/sdk-codegen-scripts/src/declarationMiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
ExampleMiner,
readFile,
getCommitHash,
getRemoteHttpOrigin,
getRemoteOrigin,
} from './exampleMiner'

/**
Expand Down Expand Up @@ -94,7 +94,8 @@ export class DeclarationMiner {
constructor(
public readonly sourcePath: string,
public readonly methodProbe: IProbe,
public readonly typeProbe: IProbe
public readonly typeProbe: IProbe,
private readonly originOverride: string
) {}

execute(sourcePath?: string, methodProbe?: IProbe, typeProbe?: IProbe) {
Expand Down Expand Up @@ -127,8 +128,11 @@ export class DeclarationMiner {
}

get remoteOrigin(): string {
if (this.originOverride) {
return this.originOverride
}
process.chdir(this.sourcePath)
return getRemoteHttpOrigin()
return getRemoteOrigin()
}

get lode(): IDeclarationMine {
Expand Down
20 changes: 18 additions & 2 deletions packages/sdk-codegen-scripts/src/exampleMiner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,23 @@ export const filterCodeFiles = (fileName: string) => {
return ext in fileMiners
}

const skipFolder = (name: string, excludeList: string[] = ['node_modules']) =>
export const IGNORE_PATHS = [
'node_modules',
'lib',
'dist',
'bazel-bin',
'build',
'bin',
'.build',
'.direnv',
'.github',
'.vscode',
'.idea',
'.gradle',
'results',
]

const skipFolder = (name: string, excludeList: string[] = IGNORE_PATHS) =>
new RegExp(excludeList.join('|'), 'gi').test(name)

/**
Expand Down Expand Up @@ -133,7 +149,7 @@ export const getCodeFiles = (
searchPath: string,
listOfFiles: string[] = [],
filter: FileFilter = filterCodeFiles,
ignorePaths: string[] = ['node_modules', 'lib', 'dist', 'bazel-bin']
ignorePaths: string[] = IGNORE_PATHS
) => {
return getAllFiles(searchPath, listOfFiles, filter, ignorePaths)
}
Expand Down
9 changes: 5 additions & 4 deletions packages/sdk-codegen/src/declarationInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
SOFTWARE.
*/
import type { IMine, IFileCall } from './exampleInfo'
import type { IMine, IFileCall, SourceLink } from './exampleInfo'
import { permaLink } from './exampleInfo'
import type { KeyedCollection } from './sdkModels'

Expand All @@ -43,11 +43,13 @@ export type DeclarationNuggets = KeyedCollection<IDeclarationNugget>
* @param lode All declaration data
* @param methodId Method id to search for
* @param typeId Type id to search for
* @param sourcerer Function to format source code link
*/
export const findDeclaration = (
lode: IDeclarationMine,
methodId?: string,
typeId?: string
typeId?: string,
sourcerer: SourceLink = permaLink
) => {
let declaration
if (methodId) {
Expand All @@ -58,8 +60,7 @@ export const findDeclaration = (

let link
if (declaration) {
// TODO make link configurable
link = permaLink(
link = sourcerer(
lode.remoteOrigin,
lode.commitHash,
declaration.sourceFile,
Expand Down
24 changes: 23 additions & 1 deletion packages/sdk-codegen/src/exampleInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,43 @@ export interface IExampleMine extends IMine {
nuggets: Nuggets
}

/** function type for formatting source code links */
export type SourceLink = (
remote: string,
hash: string,
fileName: string,
line: number
) => string

/**
* Create a permalink for a github file with line number
* @param remote https url for repository
* @param hash commit hash
* @param fileName name of file
* @param line line number in file
*/
export const permaLink = (
export const permaLink: SourceLink = (
// https://github.com/looker-open-source/sdk-codegen/blob/bfca52d2c8ba85018f76548158fd1dd90aa1f64a/examples/typescript/customConfigReader.ts#L53
remote: string,
hash: string,
fileName: string,
line: number
) => `${remote}/blob/${hash}/${fileName}#L${line}`

/**
* Create a permalink for a code search with commit hash and line number
* @param remote https url for repository
* @param hash commit hash
* @param fileName name of file
* @param line line number in file
*/
export const codeSearchLink: SourceLink = (
remote: string,
hash: string,
fileName: string,
line: number
) => `${remote}/${fileName}?l=${line}&rcl=${hash}`

/**
* Create an IDE file link with line number
* @param parentPath to fully qualify the source file
Expand Down

0 comments on commit d75bce8

Please sign in to comment.