Skip to content

Commit

Permalink
Merge branch 'main' into improve-snapshots-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Anshgrover23 authored Feb 11, 2025
2 parents c22b017 + 2b6849e commit 2915b6c
Show file tree
Hide file tree
Showing 34 changed files with 933 additions and 217 deletions.
53 changes: 25 additions & 28 deletions .github/workflows/bundle-size.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ jobs:
- name: Get base bundle size
id: base-bundle-size
run: |
echo "base=$(du -sh dist | cut -f1)" >> $GITHUB_OUTPUT
echo "base=$(du -sk dist | cut -f1)" >> $GITHUB_OUTPUT
- name: Get base install size
id: base-install-size
run: |
echo "base=$(bunx howfat -r simple . | grep 'Size:' | awk '{print $2}')" >> $GITHUB_OUTPUT
echo "base=$(bunx howfat -r simple . | grep 'Size:' | awk '{print $2}' | sed 's/K//')" >> $GITHUB_OUTPUT
- name: Checkout PR branch
uses: actions/checkout@v3
Expand All @@ -55,41 +55,38 @@ jobs:
- name: Get PR bundle size
id: pr-bundle-size
run: |
echo "pr=$(du -sh dist | cut -f1)" >> $GITHUB_OUTPUT
echo "pr=$(du -sk dist | cut -f1)" >> $GITHUB_OUTPUT
- name: Get PR install size and full howfat output
id: pr-install-size
run: |
echo "pr=$(bunx howfat -r simple . | grep 'Size:' | awk '{print $2}')" >> $GITHUB_OUTPUT
echo "pr=$(bunx howfat -r simple . | grep 'Size:' | awk '{print $2}' | sed 's/K//')" >> $GITHUB_OUTPUT
echo "full_output<<EOF" >> $GITHUB_OUTPUT
bunx howfat -r table . >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Calculate bundle size difference
id: bundle-size-diff
run: |
base_size=$(echo ${{ steps.base-bundle-size.outputs.base }} | sed 's/[^0-9.]*//g')
pr_size=$(echo ${{ steps.pr-bundle-size.outputs.pr }} | sed 's/[^0-9.]*//g')
diff=$(echo "$pr_size - $base_size" | bc)
if (( $(echo "$diff > 0" | bc -l) )); then
echo "diff=+$diff" >> $GITHUB_OUTPUT
else
echo "diff=$diff" >> $GITHUB_OUTPUT
fi
base_size=${{ steps.base-bundle-size.outputs.base }}
pr_size=${{ steps.pr-bundle-size.outputs.pr }}
diff=$((pr_size - base_size))
echo "diff=$diff" >> $GITHUB_OUTPUT
- name: Calculate install size difference
id: install-size-diff
run: |
base_size=$(echo ${{ steps.base-install-size.outputs.base }} | sed 's/[^0-9.]*//g')
pr_size=$(echo ${{ steps.pr-install-size.outputs.pr }} | sed 's/[^0-9.]*//g')
base_size=${{ steps.base-install-size.outputs.base }}
pr_size=${{ steps.pr-install-size.outputs.pr }}
diff=$(echo "$pr_size - $base_size" | bc)
if (( $(echo "$diff > 0" | bc -l) )); then
echo "diff=+$diff" >> $GITHUB_OUTPUT
else
echo "diff=$diff" >> $GITHUB_OUTPUT
fi
- name: Update comment
echo "diff=$diff" >> $GITHUB_OUTPUT
- name: Conditionally Update PR Comment
if: |
steps.bundle-size-diff.outputs.diff | tonumber >= 10
or steps.bundle-size-diff.outputs.diff | tonumber <= -10
or steps.install-size-diff.outputs.diff | tonumber >= 10
or steps.install-size-diff.outputs.diff | tonumber <= -10
uses: mshick/add-pr-comment@v2
env:
GITHUB_TOKEN: ${{ secrets.TSCIRCUIT_BOT_GITHUB_TOKEN }}
Expand All @@ -98,14 +95,14 @@ jobs:
message: |
## Size Report
### Bundle Size
- Base branch size: ${{ steps.base-bundle-size.outputs.base }}
- PR branch size: ${{ steps.pr-bundle-size.outputs.pr }}
- Difference: ${{ steps.bundle-size-diff.outputs.diff }}
- Base branch size: ${{ steps.base-bundle-size.outputs.base }} KB
- PR branch size: ${{ steps.pr-bundle-size.outputs.pr }} KB
- Difference: ${{ steps.bundle-size-diff.outputs.diff }} KB
### Install Size
- Base branch size: ${{ steps.base-install-size.outputs.base }}
- PR branch size: ${{ steps.pr-install-size.outputs.pr }}
- Difference: ${{ steps.install-size-diff.outputs.diff }}
- Base branch size: ${{ steps.base-install-size.outputs.base }} KB
- PR branch size: ${{ steps.pr-install-size.outputs.pr }} KB
- Difference: ${{ steps.install-size-diff.outputs.diff }} KB
### Full Howfat Output (PR Branch)
```
Expand All @@ -114,4 +111,4 @@ jobs:
(aside)
```
proxy-url: https://add-pr-comment-proxy-tscircuit.vercel.app/api
allow-repeats: false
allow-repeats: false
Binary file modified bun.lockb
Binary file not shown.
8 changes: 7 additions & 1 deletion lib/components/base-components/PrimitiveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ export abstract class PrimitiveComponent<
* schematic components
*/
computeSchematicPropsTransform(): Matrix {
return compose(translate(this.props.schX ?? 0, this.props.schY ?? 0))
const { _parsedProps: props } = this
return compose(translate(props.schX ?? 0, props.schY ?? 0))
}

/**
Expand Down Expand Up @@ -465,6 +466,11 @@ export abstract class PrimitiveComponent<
}

add(component: PrimitiveComponent) {
if (!component.onAddToParent) {
throw new Error(
`Invalid JSX Element: Expected a React component but received "${JSON.stringify(component)}"`,
)
}
component.onAddToParent(this)
component.parent = this
this.children.push(component)
Expand Down
5 changes: 3 additions & 2 deletions lib/components/normal-components/Capacitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export class Capacitor extends NormalComponent<
get config() {
return {
componentName: "Capacitor",
schematicSymbolName:
this.props.symbolName ?? ("capacitor" as BaseSymbolName),
schematicSymbolName: this.props.polarized
? "capacitor_polarized"
: (this.props.symbolName ?? ("capacitor" as BaseSymbolName)),
zodProps: capacitorProps,
sourceFtype: FTYPE.simple_capacitor,
}
Expand Down
1 change: 1 addition & 0 deletions lib/components/normal-components/Chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class Chip<PinLabels extends string = never> extends NormalComponent<
layer: props.layer ?? "top",
rotation: props.pcbRotation ?? 0,
source_component_id: this.source_component_id!,
subcircuit_id: this.getSubcircuit().subcircuit_id ?? undefined,
})

this.pcb_component_id = pcb_component.pcb_component_id
Expand Down
1 change: 1 addition & 0 deletions lib/components/normal-components/Jumper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export class Jumper<PinLabels extends string = never> extends NormalComponent<
layer: props.layer ?? "top",
rotation: props.pcbRotation ?? 0,
source_component_id: this.source_component_id!,
subcircuit_id: this.getSubcircuit().subcircuit_id ?? undefined,
})

this.pcb_component_id = pcb_component.pcb_component_id
Expand Down
4 changes: 2 additions & 2 deletions lib/components/normal-components/Transistor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { BaseSymbolName } from "lib/utils/constants"
export class Transistor extends NormalComponent<typeof transistorProps> {
get config() {
const baseSymbolName: BaseSymbolName =
this.props.transistorType === "npn"
this.props.type === "npn"
? "npn_bipolar_transistor"
: "pnp_bipolar_transistor"

Expand All @@ -23,7 +23,7 @@ export class Transistor extends NormalComponent<typeof transistorProps> {
const source_component = db.source_component.insert({
ftype: "simple_transistor",
name: props.name,
transistor_type: props.transistorType,
transistor_type: props.type,
} as any)
this.source_component_id = source_component.source_component_id
}
Expand Down
85 changes: 68 additions & 17 deletions lib/components/primitive-components/Group/Group.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { type SubcircuitGroupProps, groupProps } from "@tscircuit/props"
import {
type AutorouterConfig,
type SubcircuitGroupProps,
groupProps,
} from "@tscircuit/props"
import * as SAL from "@tscircuit/schematic-autolayout"
import {
type PcbTrace,
Expand Down Expand Up @@ -163,22 +167,27 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
}

_shouldRouteAsync(): boolean {
const props = this._parsedProps as SubcircuitGroupProps
if (props.autorouter === "auto-local") return true
if (props.autorouter === "auto-cloud") return true
if (props.autorouter === "sequential-trace") return false
if (typeof props.autorouter === "object") return true
return false
const autorouter = this._getAutorouterConfig()
if (autorouter.local) return false
if (autorouter.groupMode === "sequential-trace") return false
return true
}

_hasTracesToRoute(): boolean {
const debug = Debug("tscircuit:core:_hasTracesToRoute")
const traces = this.selectAll("trace") as Trace[]
debug(`[${this.getString()}] has ${traces.length} traces to route`)
return traces.length > 0
}

async _runEffectMakeHttpAutoroutingRequest() {
const debug = Debug("tscircuit:core:_runEffectMakeHttpAutoroutingRequest")
const props = this._parsedProps as SubcircuitGroupProps

const serverUrl =
(props.autorouter as any)?.serverUrl ??
"https://registry-api.tscircuit.com"
const serverMode = (props.autorouter as any)?.serverMode ?? "job"
const autorouterConfig = this._getAutorouterConfig()

const serverUrl = autorouterConfig.serverUrl!
const serverMode = autorouterConfig.serverMode!

const fetchWithDebug = (url: string, options: RequestInit) => {
debug("fetching", url)
Expand Down Expand Up @@ -239,6 +248,7 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
autostart: true,
display_name: this.root?.name,
subcircuit_id: this.subcircuit_id,
server_cache_enabled: autorouterConfig.serverCacheEnabled,
}),
headers: { "Content-Type": "application/json" },
},
Expand Down Expand Up @@ -307,6 +317,7 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>

doInitialPcbTraceRender() {
const debug = Debug("tscircuit:core:doInitialPcbTraceRender")
if (!this.isSubcircuit) return
if (this.root?.pcbDisabled) return
if (this._shouldUseTraceByTraceRouting()) return

Expand All @@ -320,12 +331,18 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
debug(
`[${this.getString()}] no child subcircuits to wait for, initiating async routing`,
)
if (!this._hasTracesToRoute()) return
this._startAsyncAutorouting()
}

updatePcbTraceRender() {
const debug = Debug("tscircuit:core:updatePcbTraceRender")
if (this._shouldRouteAsync() && !this._hasStartedAsyncAutorouting) {
if (!this.isSubcircuit) return
if (
this._shouldRouteAsync() &&
this._hasTracesToRoute() &&
!this._hasStartedAsyncAutorouting
) {
if (this._areChildSubcircuitsRouted()) {
debug(
`[${this.getString()}] child subcircuits are now routed, starting async autorouting`,
Expand Down Expand Up @@ -449,6 +466,42 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
SAL.mutateSoupForScene(db.toArray(), laidOutScene)
}

_getAutorouterConfig(): AutorouterConfig {
const defaults = {
serverUrl: "https://registry-api.tscircuit.com",
serverMode: "job",
serverCacheEnabled: false,
}
// Inherit from parent if not set by props
const autorouter =
this._parsedProps.autorouter ?? this.getInheritedProperty("autorouter")

if (typeof autorouter === "object") {
return {
local: !(
autorouter.serverUrl ||
autorouter.serverMode ||
autorouter.serverCacheEnabled
),
...defaults,
...autorouter,
}
}

if (autorouter === "auto-local")
return {
local: true,
}
if (autorouter === "sequential-trace")
return {
local: true,
groupMode: "sequential-trace",
}
return {
local: true,
groupMode: "sequential-trace",
}
}
/**
* Trace-by-trace autorouting is where each trace routes itself in a well-known
* order. It's the most deterministic way to autoroute, because a new trace
Expand All @@ -458,10 +511,8 @@ export class Group<Props extends z.ZodType<any, any, any> = typeof groupProps>
* or if using a "fullview" or "rip and replace" autorouting mode
*/
_shouldUseTraceByTraceRouting(): boolean {
const props = this._parsedProps as SubcircuitGroupProps
if (props.autorouter === "auto-local") return true
if (props.autorouter === "sequential-trace") return true
if (props.autorouter) return false
return true
// Inherit from parent if not set by props
const autorouter = this._getAutorouterConfig()
return autorouter.groupMode === "sequential-trace"
}
}
11 changes: 10 additions & 1 deletion lib/components/primitive-components/Port/Port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,18 @@ export class Port extends PrimitiveComponent<typeof portProps> {

_getGlobalPcbPositionBeforeLayout(): { x: number; y: number } {
const matchedPcbElm = this.matchedComponents.find((c) => c.isPcbPrimitive)
const parentComponent = this.parent

// First check if parent component has a footprint
if (parentComponent && !parentComponent.props.footprint) {
throw new Error(
`${parentComponent.componentName} "${parentComponent.props.name}" does not have a footprint. Add a footprint prop, e.g. <${parentComponent.componentName.toLowerCase()} footprint="..." />`,
)
}

if (!matchedPcbElm) {
throw new Error(
`Port ${this} has no matched pcb component, can't get global pcb position`,
`Port ${this} has no matching PCB primitives. This often means the footprint's pads lack matching port hints.`,
)
}

Expand Down Expand Up @@ -258,6 +266,7 @@ export class Port extends PrimitiveComponent<typeof portProps> {
pin_number: props.pinNumber,
port_hints,
source_component_id: this.parent?.source_component_id!,
subcircuit_id: this.getSubcircuit()?.subcircuit_id!,
})

this.source_port_id = source_port.source_port_id
Expand Down
Loading

0 comments on commit 2915b6c

Please sign in to comment.