Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ GitHub Action Usage Example: https://github.com/ksimuk/codio-test-publish/blob/m
Truncate pages from project. This method creates in the `dstDir` reduced version of the project,
which contains only pages specified in `sections: string[]` and files specified in `paths: string[]`
```
await codio.v1.tools.reduce(srcDir, dstDir, sections, paths)
await codio.v1.tools.reduce(srcDir, dstDir, sections, paths, sectionsConfig)
```

## Reduce Publish
Expand All @@ -74,6 +74,7 @@ Similar to reduce but publishes generated projects as assignments.
`assignmentName` - or name of the assignment to publish
`section` - section name or array of paths to the section
`paths` - an array of files that needs to be exported, `.guides` is exported fully to all assignments
`withChildren` - boolean - preserve children structure, `true` by default

```
- assignment: <assignment Id>
Expand All @@ -86,6 +87,10 @@ Similar to reduce but publishes generated projects as assignments.
- assignmentName: <assignment name>
section: Section 1

- assignmentName: <assignment name>
section: Section 2
withChildren: false

```

GitHub Action: https://github.com/codio/codio-assignment-publish-action
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "codio-api-js",
"version": "0.17.1",
"version": "0.18.0",
"description": "JS client to Codio API",
"repository": "https://github.com/codio/codio-api-js",
"author": "Max Kraev <maxim.kraev@gmail.com>",
Expand Down
23 changes: 20 additions & 3 deletions src/lib/assignment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@ type YamlRaw = {
assignmentName: string | undefined
paths: (string | PathMap)[] | undefined
section: string | string[]
withChildren: boolean | undefined
}

export type SectionConfig = {
withChildren: boolean
}

export function sectionToKey(section: string[]) {
return section.join('\n')
}

type Yaml = {
assignment: string | undefined
assignmentName: string | undefined
paths: (string | PathMap)[]
section: string[][]
sectionConfig: Map<string, SectionConfig>
}

export type TimeExtension = {
Expand Down Expand Up @@ -192,19 +202,24 @@ function validityState(ymls: YamlRaw[]): Yaml[] {
if (assignmentId === undefined) {
throw new Error('assignment and assignmentName does not exist')
}
const withChildren = yml.withChildren !== false
if (map.has(assignmentId)) {
const item = map.get(assignmentId)
if (!item) {
continue
}
item.section.push(section)
item.paths = item.paths.concat(yml.paths || [])
item.sectionConfig.set(sectionToKey(section), {withChildren: withChildren})
} else {
const sectionConfig: Map<string, SectionConfig> = new Map()
sectionConfig.set(sectionToKey(section), {withChildren: withChildren})
map.set(assignmentId, {
assignment: yml.assignment,
assignmentName: yml.assignmentName,
paths: yml.paths || [],
section: [section]
section: [section],
sectionConfig: sectionConfig
})
}
}
Expand All @@ -228,12 +243,14 @@ function validateYmlCfg(ymls: Yaml[]): Yaml[] {
}
item.section = item.section.concat(section)
item.paths = item.paths.concat(yml.paths || [])
item.sectionConfig = yml.sectionConfig
} else {
map.set(assignmentId, {
assignment: yml.assignment,
assignmentName: yml.assignmentName,
paths: yml.paths || [],
section: section
section: section,
sectionConfig: yml.sectionConfig
})
}
}
Expand Down Expand Up @@ -296,7 +313,7 @@ async function reducePublish(courseId: string, srcDir: string, yamlDir: string,
if (!item.assignment) {
throw new Error(`assignment not found with name "${item.assignmentName}"`)
}
await tools.reduce(srcDir, tmpDstDir, item.section, _.compact(paths))
await tools.reduce(srcDir, tmpDstDir, item.section, _.compact(paths), item.sectionConfig)
await assignment.publish(courseId, item.assignment, tmpDstDir, changelogOrOptions)
fs.rmSync(tmpDstDir, {recursive: true})
}
Expand Down
20 changes: 12 additions & 8 deletions src/lib/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {excludePaths} from './config'
import tar from 'tar'
import { ZSTDCompress } from 'simple-zstd'
import config from './config'
import { PathMap } from './assignment'
import { PathMap, SectionConfig, sectionToKey } from './assignment'

const CONVERTER_VERSION = '4ca4944ddf9d4fe4df9697bec06cbd0a6c170419'
const GUIDES_CONTENT_DIR = '.guides/content'
Expand All @@ -22,13 +22,15 @@ export async function fixGuidesVersion(projectPath: string) {
}

export async function reduce(
srcDir: string, dstDir: string, yaml_sections: string[][], paths: (string | PathMap)[]): Promise<void> {
srcDir: string, dstDir: string, yaml_sections: string[][],
paths: (string | PathMap)[], sectionsConfig: Map<string, SectionConfig> = new Map()
): Promise<void> {
await fixGuidesVersion(srcDir)
const contentDir = path.join(srcDir, GUIDES_CONTENT_DIR)
const rootMetadataPath = path.join(contentDir, INDEX_METADATA_FILE)
const rootMetadata = readMetadataFile(rootMetadataPath)
const guidesStructure = getGuidesStructure(rootMetadata, srcDir, '')
const filter = collectFilter(guidesStructure, _.cloneDeep(yaml_sections))
const filter = collectFilter(guidesStructure, _.cloneDeep(yaml_sections), sectionsConfig)
const strippedStructure = stripStructure(guidesStructure, filter)
const strippedSectionsIds = getStrippedSectionIds(strippedStructure)
const excludePaths = getExcludedPaths(guidesStructure, strippedSectionsIds)
Expand Down Expand Up @@ -83,7 +85,7 @@ const DEFAULT_ALL_SECTION: Section = {
children: {}
}

function collectFilter(guidesStructure, yaml_sections) {
function collectFilter(guidesStructure, yaml_sections: string[][], sectionsConfig: Map<string, SectionConfig>) {
const filterMap = {
all: false,
children: {}
Expand All @@ -93,7 +95,9 @@ function collectFilter(guidesStructure, yaml_sections) {
if (sectionPath.length === 0) {
continue
}
const section = traverseItems(guidesStructure, sectionPath, filterMap)
const key = sectionToKey(sectionPath)
const withChildren = sectionsConfig.has(key) ? sectionsConfig.get(key)?.withChildren : true
const section = traverseItems(guidesStructure, sectionPath, filterMap, withChildren??true)
if (!section) {
throw new Error(`${section} not found`)
}
Expand All @@ -105,7 +109,7 @@ function collectFilter(guidesStructure, yaml_sections) {
return filterMap
}

function traverseItems(structure, sectionPath: string[], filterMap: Section) {
function traverseItems(structure, sectionPath: string[], filterMap: Section, withChildren: boolean) {
const sectionName = sectionPath.shift()
if (!sectionName) {
return
Expand All @@ -122,9 +126,9 @@ function traverseItems(structure, sectionPath: string[], filterMap: Section) {
}
if (sectionPath.length > 0) {
// fill-in filterMap
traverseItems(section.children, sectionPath, filterMap.children[section.id])
traverseItems(section.children, sectionPath, filterMap.children[section.id], withChildren)
} else {
filterMap.children[section.id].all = true
filterMap.children[section.id].all = withChildren
}
return section
}
Expand Down