Skip to content

Exporting Squeak Categories

Fabio Niephaus edited this page Jul 26, 2015 · 3 revisions

Example: Exporting Morphic-* Categories

| path |
path := FileList2 modalFolderSelector.
path ifNotNil: [
    Class allSubInstancesDo: [:c | 
            (c category startsWith: 'Morphic-') ifTrue: [
            | categoryName categoryDir classDir classFile |
            categoryName := ((c category copyFrom: 9 to: (c category size)) copyReplaceAll: ' ' with: '') copyReplaceAll: '-' with: ''.
            categoryDir := path directoryNamed: categoryName, FileDirectory pathNameDelimiter, 'class'.
            categoryDir exists ifFalse: [
                | categoryFile |
                [ categoryFile := path fileNamed: categoryName, '.st'.
                categoryFile nextPutAll: categoryName, Character cr, Character tab.
                categoryFile nextPutAll: '< class >', Character cr, Character tab.
                categoryFile nextPutAll: '^ Object subclass' ] ensure: [ categoryFile close ].
                path createDirectory: categoryName.
                path createDirectory: categoryName, FileDirectory pathNameDelimiter, 'class' ].
            categoryDir createDirectory: c name.
            classDir := categoryDir directoryNamed: c name.
            [ | def |
            classFile := categoryDir fileNamed: c name, '.st'.
            def := c classDefinitions first.
            classFile nextPutAll: def className, Character cr, Character tab.
            classFile nextPutAll: '< class >', Character cr, Character tab.
            classFile nextPutAll: '^ ', def superclassName, ' subclassWithInstVars: ''', def instanceVariablesString, ''' classVars: ''', def classVariablesString, '''' ] ensure: [ classFile close ].
            
            c methodDict ifNotEmpty: [
                | instanceDir |
                classDir createDirectory: 'instance'.
                instanceDir := classDir directoryNamed: 'instance'.
                c methodDict valuesDo: [:method |
                    | file |
                    [ file := instanceDir fileNamed: (method selector copyReplaceAll: ':' with: '.'), '.st'.
                    file nextPutAll: method getSource ] ensure: [ file close ]] ].
            c class methodDict ifNotEmpty: [
                | classSubDir |
                classDir createDirectory: 'class'.
                classSubDir := classDir directoryNamed: 'class'.
                c class methodDict valuesDo: [:method |
                    | file |
                    [ file := classSubDir fileNamed: (method selector copyReplaceAll: ':' with: '.'), '.st'.
                    file nextPutAll: method getSource ] ensure: [ file close ]] ].
         ]]]

Example: Exporting Files-* Categories

| path |
path := FileList2 modalFolderSelector.
path ifNotNil: [
    Class allSubInstancesDo: [:c | 
            (c category startsWith: 'Files-') ifTrue: [
            | categoryName categoryDir classDir classFile |
            categoryName := ((c category copyFrom: 'Files-' size to: (c category size)) copyReplaceAll: ' ' with: '') copyReplaceAll: '-' with: ''.
        categoryName ifEmpty: [1 halt].
            categoryDir := path directoryNamed: categoryName, FileDirectory pathNameDelimiter, 'class'.
            categoryDir exists ifFalse: [
                | categoryFile |
                [ categoryFile := path fileNamed: categoryName, '.st'.
                categoryFile nextPutAll: categoryName, Character cr, Character tab.
                categoryFile nextPutAll: '< class >', Character cr, Character tab.
                categoryFile nextPutAll: '^ Object subclass' ] ensure: [ categoryFile close ].
                path createDirectory: categoryName.
                path createDirectory: categoryName, FileDirectory pathNameDelimiter, 'class' ].
            categoryDir createDirectory: c name.
            classDir := categoryDir directoryNamed: c name.
            [ | def |
            classFile := categoryDir fileNamed: c name, '.st'.
            def := c classDefinitions first.
            classFile nextPutAll: def className, Character cr, Character tab.
            classFile nextPutAll: '< class >', Character cr, Character tab.
            classFile nextPutAll: '^ ', def superclassName, ' subclassWithInstVars: ''', def instanceVariablesString, ''' classVars: ''', def classVariablesString, '''' ] ensure: [ classFile close ].

            c methodDict ifNotEmpty: [
                | instanceDir |
                classDir createDirectory: 'instance'.
                instanceDir := classDir directoryNamed: 'instance'.
                c methodDict valuesDo: [:method |
                    | file |
            method selector = #/ ifFalse: [
                    [ file := instanceDir fileNamed: (method selector copyReplaceAll: ':' with: '.'), '.st'.
                    file nextPutAll: method getSource ] ensure: [ file close ]] ]].
            c class methodDict ifNotEmpty: [
                | classSubDir |
                classDir createDirectory: 'class'.
                classSubDir := classDir directoryNamed: 'class'.
                c class methodDict valuesDo: [:method |
                    | file |
              method selector = #/ ifFalse: [
                    [ file := classSubDir fileNamed: (method selector copyReplaceAll: ':' with: '.'), '.st'.
                    file nextPutAll: method getSource ] ensure: [ file close ]] ]].
         ]]]